viewDidLoad没有被调用

时间:2013-04-10 02:07:04

标签: objective-c

我正在开发一个小应用程序,我想在应用程序启动后立即显示一个警告框。我认为将代码放入viewDidLoad会导致这种情况发生,但似乎并非如此。

我希望能做的是两次警报显示。第一个是玩家1,第二个是玩家2.现在我正在努力让第一个工作。

这是接近这个的正确方法还是有更好的方法? 感谢。

- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"here...");
// Do any additional setup after loading the view.
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Player 1" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
[alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"Entered: %@",[[alertView textFieldAtIndex:0] text]);
}

视图控制器仅包含以下方法:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    BoxView* myView = [[BoxView alloc]
                      initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = myView;
    [myView release];
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

1 个答案:

答案 0 :(得分:1)

问题是viewDidLoad太早了。还没有界面!等到至少viewDidAppear:

在这里看到我非常相似的答案:

https://stackoverflow.com/a/15867468/341994