我有一个带有文本字段的警报视图
UIAlertView* dialog = [[UIAlertView alloc] init];
dialog.tag = 5;
[dialog setDelegate:self];
dialog.delegate = self;
[dialog setTitle:@"Please set the quantity:"];
[dialog setMessage:@"Quantity"];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"SET"];
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
//nameField.keyboardType = UIKeyboardTypeDecimalPad;
NSString *str = nameField.text;
NSLog(@"%@",str);
[dialog addSubview:nameField];
[dialog show];
[dialog release];
[nameField release];
我试图在解除警报视图后将文本字段的值存储为
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag ==5) {
if(buttonIndex == 1)
{
UITextField* textField = (UITextField*)[alertView.subviews objectAtIndex:2];
NSLog(@"%@",textField.text);
}
}
}
但是文本字段返回null,我应该给出的值或objectatindex是什么值?
感谢。
答案 0 :(得分:1)
试试这个
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag ==5) {
if(buttonIndex == 1)
{
//UITextField* textField = (UITextField*)[alertView.subviews objectAtIndex:2];
//NSLog(@"%@",textField.text);
for (UIView *subview in [alertView subviews])
{
if ([subview isKindOfClass:[UITextField class]])
{
UITextField * textField =(UITextField *)subview;
NSLog(@"%@",textField.text);
}
}
}
}
}
答案 1 :(得分:0)
使用textFieldAtIndex:
的方法实例方法UIAlertView
。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag ==5) {
if(buttonIndex == 1)
{
UITextField* textField = [alertView.subviews textFieldAtIndex:2]; //make sure the index of the text field is correct.
NSLog(@"%@",textField.text);
}
}
}
注意:此方法适用于iOS 5 +