我的iOS项目遇到了一个非常奇怪的问题。 我有一个带有一些标签和按钮的UIViewController,当用户进入这个视图时,我以编程方式构建了一个游戏板(它是扫雷游戏)。 我现在要做的是添加一个简单的元素(在我的例子中是一个分段控件,但我也尝试了一个按钮,并且没有工作)在底板或板的开头。
当我从Interface Builder中添加一些内容时,我可以在故事板中看到它,但是当我运行app puff时,它就消失了! 视图仅包含" old"元素和新元素未显示。 是的,我做了一个IBOutlet:
@property (weak, nonatomic) IBOutlet UISegmentedControl *clickTypeSegmentedControl;
是的,我检查了分段控制的引用是否正常。 我无法真正理解错误,如果有人可以帮助我会非常感激。
**编辑:我添加了ViewController的代码,我还遗漏了一些其他方法,因为它们只处理游戏。我再说一遍:即使我添加一个没有动作的简单按钮,它也不会显示。
#import "GameViewController.h"
#import "GameBoardModel.h"
#import "ScoreViewController.h"
#import <AVFoundation/AVFoundation.h>
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface GameViewController ()
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@property (weak, nonatomic) IBOutlet UILabel *bombsLabel;
@property (weak, nonatomic) IBOutlet UIButton *restartButton;
@property(nonatomic, strong) AVAudioPlayer *soundEffects;
@property (weak, nonatomic) IBOutlet UISegmentedControl *clickTypeSegmentedControl;
@end
@implementation GameViewController
@synthesize difficulty, timer, playerName, scoreToAdd, clickTypeSegmentedControl;
double secondsPassed=-1.0f;
int seconds=0;
int totalRowsCols=0, totalMines=0, heightWidth=0, clicKNumber=0;
static NSString * const IMAGE_NAME_FLAG = @"flag.png";
static NSString * const IMAGE_NAME_BOMB_X = @"bomb.png";
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *backButton =[[UIBarButtonItem alloc]initWithTitle:@"Menù" style:UIBarButtonItemStyleBordered target:self action:@selector(popAlertAction:)];
self.navigationItem.leftBarButtonItem=backButton;
clickTypeSegmentedControl.selectedSegmentIndex = 0;
rootController = (BombsSeekerViewController *)[self.navigationController.viewControllers objectAtIndex:0];
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
playerName = [defaults objectForKey:@"NomeGiocatore"];
scoreToAdd = [[NSMutableDictionary alloc]init];
[self createGameBoard:difficulty];
[self newGame:nil];
}
-(void) newGame:(UIButton *)sender {
[self.view.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
clicKNumber=0;
secondsPassed=0;
self.timerLabel.text = [self labelString:secondsPassed];
self.game = [[Game alloc] initWithWidth:totalRowsCols AndHeight:totalRowsCols AndMineCount:totalMines];
self.buttonArray = [[NSMutableArray alloc] init];
[self.restartButton addTarget:self action:@selector(newGame:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.restartButton];
[self.view addSubview:self.timerLabel];
[self.view addSubview:self.bombsLabel];
for(int i = 0; i < totalRowsCols; i++) {
for (int j = 0; j < totalRowsCols; j++) {
self.button = [[Tile alloc] initWithLocation: CGPointMake(j,i) andHW:heightWidth andBlock:self.game.board[j][i] andTag:(i*totalRowsCols+j)];
[self.buttonArray addObject:self.button];
[self.view addSubview:self.button];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
}
[self.timer invalidate];
}
- (void) createGameBoard:(int)diff{
switch (diff) {
case 1:
totalRowsCols = 6;
totalMines = 2;
heightWidth = 44;
break;
case 2:
totalRowsCols = 8;
totalMines = 16;
heightWidth = 35;
break;
case 3:
totalRowsCols = 10;
totalMines = 25;
heightWidth = 29;
break;
}
self.flagCount = totalMines;
_bombsLabel.text = [NSString stringWithFormat:@"%d",self.flagCount];
}
-(NSString *) labelString: (int) num {
if(num < 10) {
return [NSString stringWithFormat:@"00%i",num];
}
else if(self.timeCount.intValue < 100) {
return [NSString stringWithFormat:@"0%i",num];
}
else if(self.timeCount.intValue < 1000) {
return [NSString stringWithFormat:@"%i",num];
}
else
return @"---";
}
-(void) click:(Tile *)sender {
if(clicKNumber <1){
[self refreshLabel:self.timer];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(refreshLabel:)
userInfo:nil
repeats:YES];
clicKNumber++;
}
if(self.game.gameStatus == STATUS_LOST || self.game.gameStatus == STATUS_WON) return;
if (self.clickTypeSegmentedControl.selectedSegmentIndex == 0 && sender.block.marking == MARKING_BLANK) {
[self.game clickedAtColumn: sender.point.x AndRow: sender.point.y];
for(Tile *b in self.buttonArray) {
if(b.block.marking == MARKING_CLICKED) {
b.enabled = NO;
}
else if(b.block.marking == MARKING_BLANK) {
[b setTitle:@"" forState:UIControlStateNormal];
[b setImage:nil forState:UIControlStateNormal];
}
}
}
else if (self.clickTypeSegmentedControl.selectedSegmentIndex == 1){
if(sender.block.marking == MARKING_BLANK && self.flagCount > 0) {
sender.block.marking = MARKING_FLAGGED;
self.flagCount--;
_bombsLabel.text = [NSString stringWithFormat:@"%d",self.flagCount];
[sender setImage:[UIImage imageNamed:IMAGE_NAME_FLAG] forState:UIControlStateNormal];
}
else if(sender.block.marking == MARKING_FLAGGED) {
sender.block.marking = MARKING_BLANK;
self.flagCount++;
_bombsLabel.text = [NSString stringWithFormat:@"%d",self.flagCount];
[sender setImage:[UIImage imageNamed:@"tile.png"] forState:UIControlStateNormal];
}
}
if(self.game.gameStatus == STATUS_LOST) [self gameLost:sender];
else if(self.game.gameStatus == STATUS_WON) [self gameWon];
}
答案 0 :(得分:0)
确保故事板中的控制器与正确的控制器类链接。
答案 1 :(得分:0)
如果这是我认为的,你不应该这样做:
[self.view.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
调用viewDidLoad
时,会调用newGame
方法,从Superview中删除子视图。包括在Storyboard中添加的那些。
答案 2 :(得分:0)
您在-viewDidLoad:
-(void) newGame:(UIButton *)sender {
[self.view.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
//...
}
因此,放入笔尖的任何东西都会在加载后被移除。