您好我想将从警报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
答案 0 :(得分:0)
替换评论
//capture the value of the UITextField
使用以下行:
[self.exampleMessages addObject:userMessage.text ?: @""];
在此之后,您需要通过[self.tableView reloadData]
或类似的电话更新表格视图。