从UIAlertView获取文本 [英] Taking Text From a UIAlertView

查看:80
本文介绍了从UIAlertView获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎缺少某些内容,但以下代码为 title 和<$生成 nil 值c $ c> title1 (即使它正确启动了正确的警报类型并且没有指出任何警告或错误)。这个 UIAlertView 的实现可能会出现什么问题?

There seems to be something missing, but the below code is generating nil values for both title and title1 (even though it launches the right alert type correctly and doesn't indicate any warning or error). What could be the problem with this implementation of 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]);


推荐答案

在代码中的某处显示警报并设置视图控制器从它被呈现为您的UIAlertView的委托。然后实现委托接收事件。

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];

实施委托方法

- (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]);
}

这篇关于从UIAlertView获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆