0

我有一个带有文本字段的警报视图

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];

我试图在 alertviews 解雇后将文本字段的值存储为

- (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 是什么?

谢谢。

4

2 回答 2

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);
            }
        }


    }
}
}
于 2012-05-18T10:41:55.037 回答
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+

于 2012-05-18T10:30:29.817 回答