0

私は小さなアプリケーションに取り組んでおり、アプリケーションが起動されるとすぐにアラート ボックスが表示されるようにしたいと考えています。コードをviewDidLoadに入れるとこれが起こると思いましたが、そうではないようです。

私ができるようにしたいのは、アラートが2回表示されることです。最初のものはプレーヤー 1 用で、2 番目はプレーヤー 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.
}
4

1 に答える 1

1

問題は、それviewDidLoadが早すぎることです。インターフェースはまだありません!少なくとも まで待ちviewDidAppear:ます。

ここで私の非常によく似た答えを見てください:

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

于 2013-04-10T02:14:30.460 に答える