从UIAlertView获取文本

时间:2010-12-30 05:58:05

标签: 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];

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

编辑:好的,我想我错过了处理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];

    }

现在我只需要连接各个部分,但不确定如何。

5 个答案:

答案 0 :(得分:45)

人们在使用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];

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

答案 1 :(得分:27)

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

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

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

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

答案 2 :(得分:3)

请勿尝试在UITextView上添加UIAlertView,否则Apple可能拒绝您的应用。

我已经在我的应用中使用了相同类型的功能。但由于在UITextView中使用了UIAlertView,Apple 拒绝了我的应用

答案 3 :(得分:0)

– alertView:clickedButtonAtIndex:是UIAlertView的委托方法。只需将您在编辑中定义的方法添加到控制器实现中,您就应该好了,因为您已经将控制器设置为AlertView的委托。

还要添加到您的类标题中,以避免任何与委托相关的警告,即:

@interface MyViewController : UIViewController <UIAlertViewDelegate> {
...
};

不要忘记从插入方法中删除[data addObject:[nameField text]];[mainTableView reloadData];

答案 4 :(得分:0)

UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Alert title" 
                                                  message:@"alert message" 
                                                 delegate:self 
                                        cancelButtonTitle:@"Cancel" 
                                        otherButtonTitles:@"Ok", nil];
[myAlert addTextFieldWithValue:nil label:@"<place holder>"];
[[myAlert textField] setTextAlignment:UITextAlignmentCenter];
[[myAlert textField] becomeFirstResponder];
[myAlert show];
[myAlert release];
myAlert = nil;               

无需添加自定义文本字段。您可以使用addTextFieldWithValue方法直接添加。