我有这个代码提示UIAlertView,用UITextfield:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"New List Item", @"new_list_dialog")
message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:myTextField];
[myAlertView show];
[myAlertView release];
Run Code Online (Sandbox Code Playgroud)
但我想添加一个获取文本字段值,用户点击"确定"后,用户点击后,我想调用一个方法,如何将其分配给myAlertView?谢谢.
我正在尝试从警报视图中获取文本并将其添加到我的可变数组中以在表视图中列出.我意识到几个月前发布了类似的问题,但我不明白如何利用给定的答案.
-(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];
Run Code Online (Sandbox Code Playgroud)
然而,我的应用程序崩溃,因为它说我试图在索引0处插入一个零对象.我做错了什么?
编辑:好吧我想我错过了处理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];
}
Run Code Online (Sandbox Code Playgroud)
现在我只需要连接各个部分,但不知道如何.