【问题标题】:Getting text from UIAlertView从 UIAlertView 获取文本
【发布时间】:2010-12-30 05:58:05
【问题描述】:

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

-(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 对象。我做错了什么?

编辑:好的,我想我缺少一种处理警报视图的方法。所以我发现了这个:

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

    }

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

【问题讨论】:

    标签: iphone objective-c uialertview


    【解决方案1】:

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

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

    更新:此答案是在 Apple 创建用于通过 UIAlertViewStyleUITextField 添加到警报的 API 之前发布的。这是从@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"
        }
    }
    

    【讨论】:

    • 我的应用是一个基于窗口的应用。这有关系吗?
    • @f-Prime 不,没关系。我们正在遍历 UIAlertView 的层次结构。
    • 好的,我的完整代码应该是什么样子的?我的 add 方法加上您的 clickedButtonAtIndex 方法,它应该可以工作吗?我必须在我的 add 方法中调用 clickedButtonAtIndex 吗?因为现在这样,它不起作用
    • 您在这里唯一错过的就是重新加载表格视图。
    • 这个答案不正确。您必须设置正确的 UIAlertViewStyle,而不是添加子视图。这是危险的,不便于携带。请参阅下面的@Matt 答案。
    【解决方案2】:

    您不需要将自己的文本字段添加到 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
        }
    }
    

    【讨论】:

    • i Got name 在 IOS 11 中为零
    【解决方案3】:

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

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

    【讨论】:

    • wtf 我在应用程序中使用了很多案例,一个应用程序发布了吗?你是不是做错了什么?
    【解决方案4】:

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

    也添加到你的类头,以避免任何委托相关的警告,即:

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

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

    【讨论】:

    • 好的,我这样做了,但在 clickedButtonAtIndex 方法中出现“未声明的名称字段”错误
    【解决方案5】:
    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 方法直接添加。

    【讨论】:

    • UIAlertView 好像没有方法addTextFieldWithValue:label:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多