似乎缺少一些东西,但下面的代码正在为 nil
和 title
生成 title1
值(即使它正确启动了正确的警报类型并且没有指示任何警告或错误)。 UIAlertView
的这种实现可能有什么问题?
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]);
最佳答案
在代码中的某处显示警报并设置 View Controller ,将其显示为 UIAlertView 的委托(delegate)。然后实现委托(delegate)以接收事件。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"High Score" message:@"Score Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
alert.alertViewStyle= UIAlertViewStylePlainTextInput;
[alert show];
实现委托(delegate)方法
- (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]);
}
关于ios - 从 UIAlertView 中获取文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16171073/