从UIAlertView获取文本 [英] Getting text from UIAlertView

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

问题描述

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

I'm trying to get text from an alert view and add it to my mutable array to list in a table view. I realize there is a similar question that was posted a few months ago, but I dont understand how to utilize the given answer.

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

然而我的应用程序崩溃是因为它说我试图在索引0处插入一个nil对象。我做错了吗?

However my app crashes because it says I'm attempting to insert a nil object at index 0. What am I doing wrong?

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

Ok I think I'm missing a method to handle the alertview. So I found this:

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

    }

现在我只需要连接件,但不确定如何。

Now I just need to connect the pieces, but not sure how.

推荐答案

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

A common mistake that people make when using a UIAlertView is that they think that sending it a show message will block the main thread. It does not. Your code continues to execute, even though there is an alert on the screen. Thus, no value exists for the UITextField that you have passed in.

您需要做的是实现您的 UIViewController 中的 UIAlertViewDelegate 可以抓取用户在文本字段中输入的内容。也就是说,您仍然需要检查 nil 值,因为如果您没有输入任何内容,文本值将 nil

What you need to do is implement the UIAlertViewDelegate in your UIViewController to grab whatever a user has entered into the text field. That said, you still have to check for a nil value, because if you don't type anything in, the text value will be nil.

更新:此答案在Apple创建API以添加<$之前发布c $ c> UITextField 通过 UIAlertViewStyle 发送警报。这是更新的代码,借用@matt

UPDATE: This answer was posted before Apple created an API for adding a UITextField to an alert through the UIAlertViewStyle. Here is the updated code, borrowed from @matt

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter Name"
                                                message:@"  "   
                                               delegate:self 
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];

然后,在 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"
    }
}

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

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