【发布时间】: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