3

何かが欠けているようですが、以下のコードは と の両方の値を生成しnilています (正しいアラート タイプを正しく起動し、警告やエラーを示していません)。のこの実装の問題は何でしょうか?titletitle1UIAlertView

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"High Score" message:(NSString *)scoreMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
alert.alertViewStyle= UIAlertViewStylePlainTextInput;
UITextField *title1 = [alert textFieldAtIndex:0];
[alert show];
title1= [alert textFieldAtIndex:0];
NSString *title = title1.text;
NSLog(@"The name is %@",title);
NSLog(@"Using the Textfield: %@",[[alert textFieldAtIndex:0] text]);
4

4 に答える 4

11

Present alert somewhere in you code and set the view controller from it was presented as the delegate for your UIAlertView.Then implement the delegate to receive the event.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"High Score" message:@"Score Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
alert.alertViewStyle= UIAlertViewStylePlainTextInput;
[alert show];

Implement the delegate method

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
   UITextField *textField = [alertView textFieldAtIndex:0];
   NSString *title = textField.text;
   NSLog(@"The name is %@",title);
   NSLog(@"Using the Textfield: %@",[[alertView textFieldAtIndex:0] text]);
}
于 2013-04-23T14:15:22.353 に答える
6

[alert show]ユーザーがテキスト フィールドにテキストを入力する前に、すぐに戻ります。アラートを設定して実装することにより、アラートが閉じられたにテキストを取得する必要があります(たとえば、他にもいくつかの可能性があります)。delegatealertView:clickedButtonAtIndex:

于 2013-04-23T14:02:20.807 に答える