我的iOS项目遇到了一个非常奇怪的问题。我有一个带有一些标签和按钮的UIViewController,当用户进入这个视图时,我以编程方式构建一个游戏板(这是扫雷游戏)。我现在想做的是添加一个简单的元素(在我的情况下一个分段控制,但我也尝试了一个按钮,不工作)在底部或在板的开始。
当我从Interface Builder中添加一些东西时,我可以在故事板中看到它,但是当我运行应用程序puff时,它消失了!视图只包含"旧"元素,而不显示新元素。是的,我创建了一个IBOutlet:
@property (weak, nonatomic) IBOutlet UISegmentedControl *clickTypeSegmentedControl;
是的,我检查了分段控制的引用是否ok。我真的不明白是怎么回事,如果有人能帮助我,我将非常感激。
**编辑:我添加了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];
}
如果这是我认为的,你不应该这样做:
[self.view.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
当viewDidLoad
被调用时,newGame
方法被调用,这将从Superview中删除所有子视图。
确保storyboard中的控制器与正确的控制器类相关联
在-viewDidLoad:
期间调用此方法
-(void) newGame:(UIButton *)sender {
[self.view.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
//...
}
因此,你在nib中放入的任何东西在加载后都会被删除。