【问题标题】:insert UITextField (from a UIAlertController) into an NSMutableArray将 UITextField(来自 UIAlertController)插入 NSMutableArray
【发布时间】:2016-02-18 21:13:29
【问题描述】:

您好,我想将从警报 txtfield 获得的用户输入插入到可变数组中。

所以,我有一个带有按钮的 tableView 来添加额外的行。当按下按钮时,用户可以输入消息。

我想将该消息插入到我的数组中。

我打算使用方法 clickedButtonAtIndex 但 Xcode 声明该方法现在已弃用。

还有其他方法可以做到这一点吗??

这是我的数组的代码:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.exampleMessages = [[NSMutableArray alloc]initWithObjects:@"Example Message..", @"Example Message..",@"Example Message..",@"Example Message..",@"Example Message..", @"Example Message..",@"Example Message..",@"Example Message..", nil];}

这是我按下添加行按钮后执行的函数:

- (void)insertNewObject
//function that is executed after a new object is inserted
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
                                message:nil
                                preferredStyle:UIAlertControllerStyleAlert];
    //create a UIAlert named alert

    [alert addTextFieldWithConfigurationHandler:^(UITextField *messageTextField) {
         messageTextField.placeholder = NSLocalizedString(@"message content", @"Message");
    }];
    //add a UITextField to the UIAlert

    UIAlertAction *messageAction = [UIAlertAction
                                    actionWithTitle:NSLocalizedString(@"Add", @"Message action")
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action) {
                                        UITextField *userMessage = alert.textFields.firstObject;
                                        NSLog(@"%@", userMessage);
                                        //capture the value of the UITextField
                                    }];
    //add a button called "add"

    UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action) {}];
    //add a button called "cancel"

    [alert addAction:messageAction];
    //add the add button to the alert
    [alert addAction:cancelAction];
    //add the cancel button to the alert
    [self presentViewController:alert animated:YES completion:nil];
    //present the alert to the UI
}

在此处的代码中,我正在捕获值并将其存储在变量中,但现在如何将其放入数组中?

 UITextField *userMessage = alert.textFields.firstObject;
                                    NSLog(@"%@", userMessage);
                                    //capture the value of the UITextField

【问题讨论】:

    标签: objective-c uitableview uiviewcontroller uitextfield nsmutablearray


    【解决方案1】:

    替换评论

    //capture the value of the UITextField
    

    下面一行:

    [self.exampleMessages addObject:userMessage.text ?: @""];
    

    看起来之后您需要通过[self.tableView reloadData] 或类似的调用来更新表格视图。

    【讨论】:

    • hhmmm 我知道它是如何工作的,但是当我将它添加到我的代码中时,什么也没发生。该消息未添加到我的数组中。我是否需要更新我的阵列?像在添加消息之后?
    • 我刚刚编辑了我的答案。如果这没有帮助,我建议在将文本添加到数组后立即检查 self.exampleMessages 属性值。 F.e.添加NSLog(@"%@", self.exampleMessages) 并查看日志。
    • 我在您更新评论之前看到了您的评论,重新加载表格视图有效!
    猜你喜欢
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多