从UIAlertView获取文本

Sno*_*man 17 iphone objective-c uialertview

我正在尝试从警报视图中获取文本并将其添加到我的可变数组中以在表视图中列出.我意识到几个月前发布了类似的问题,但我不明白如何利用给定的答案.

-(IBAction)insert {
    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:self];
    [dialog setTitle:@"Enter Name"];
    [dialog setMessage:@" "];
    [dialog addButtonWithTitle:@"Cancel"];
    [dialog addButtonWithTitle:@"OK"];

    UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
    [nameField setBackgroundColor:[UIColor whiteColor]];
    [dialog addSubview:nameField];
    [dialog show];

    [data addObject:[nameField text]];
    [mainTableView reloadData];
Run Code Online (Sandbox Code Playgroud)

然而,我的应用程序崩溃,因为它说我试图在索引0处插入一个零对象.我做错了什么?

编辑:好吧我想我错过了处理alertview的方法.所以我发现了这个:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
    if([buttonTitle isEqualToString:@"Cancel"]) {
        return;
    }
    else if([buttonTitle isEqualToString:@"Ok"]) {
        [data addObject:nameField.text];

    }
Run Code Online (Sandbox Code Playgroud)

现在我只需要连接各个部分,但不知道如何.

Way*_*man 45

人们在使用a时常犯的一个错误UIAlertView是他们认为发送show消息会阻塞主线程.它不是.即使屏幕上有警报,您的代码也会继续执行.因此,UITextField您传入的值不存在任何值.

您需要做的是实现UIAlertViewDelegate在您UIViewController的文本字段中抓取用户输入的内容.也就是说,你仍然需要检查一个nil值,因为如果你没有输入任何内容,那么text值就是nil.

更新:这个答案是在Apple创建了一个API之前发布的,用于通过UITextField提醒添加警报UIAlertViewStyle.这是从@matt借来的更新代码

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter Name"
                                                message:@"  "   
                                               delegate:self 
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
Run Code Online (Sandbox Code Playgroud)

然后,在UIAlertViewDelegate回拨中:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSString *name = [alertView textFieldAtIndex:0].text;
        // Insert whatever needs to be done with "name"
    }
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*att 27

您不需要将自己的文本字段添加到AlertView,而是设置适当的样式

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter Name"
                                                message:@"  "   
                                               delegate:self 
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
Run Code Online (Sandbox Code Playgroud)

然后在按下确定(按钮1)后访问输入的文本

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSString *name = [alertView textFieldAtIndex:0].text;
        // name contains the entered value
    }
}
Run Code Online (Sandbox Code Playgroud)