-1

得到以下代码:

-(void)addGrade {

    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Text" message:@"\n \n \n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];

    // Textfield1

    UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];

    [utextfield becomeFirstResponder];

    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];

    utextfield.leftView = paddingView;
    utextfield.leftViewMode = UITextFieldViewModeAlways;

    utextfield.placeholder = @"Subject";
    [utextfield setBackgroundColor:[UIColor whiteColor]];
    utextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [alertview addSubview:utextfield];


    // Textfield2

    UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 90.0, 25.0)];

    ptextfield.placeholder = @"Another Subject";
    ptextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    ptextfield.textAlignment = NSTextAlignmentCenter;

    [ptextfield setBackgroundColor:[UIColor whiteColor]];
    [alertview addSubview:ptextfield];

    // Textfield3

    UITextField *ctextfield = [[UITextField alloc] initWithFrame:CGRectMake(161.0, 85.0, 27.0, 25.0)];

    ctextfield.placeholder = @"3";
    [ctextfield setBackgroundColor:[UIColor whiteColor]];
    ctextfield.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    ctextfield.textAlignment = NSTextAlignmentCenter;
    [alertview addSubview:ctextfield];


    // Label1

    UILabel *telt = [[UILabel alloc] initWithFrame:CGRectMake(121.0, 85.0, 40.0, 25.0)];

    telt.text = @"Text";
    telt.textColor = [UIColor whiteColor];
    telt.backgroundColor = [UIColor clearColor];
    [alertview addSubview:telt];

    // Label2

    UILabel *keermee = [[UILabel alloc] initWithFrame:CGRectMake(199.0, 85.0, 100.0, 25.0)];

    keermee.text = @"more text";
    keermee.textColor = [UIColor whiteColor];
    keermee.backgroundColor = [UIColor clearColor];
    [alertview addSubview:keermee];

    [alertview show];

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {


    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if ([title isEqualToString:@"Add"])
    {
        UITextField *field = (UITextField *)[[alertView subviews] lastObject];
        NSLog (@"%@", field.text);

    }




}

此代码在 UIAlertView 中生成 3 个文本字段和 2 个标签。

随着UITextField *field = (UITextField *)[[alertView subviews] lastObject];我回来了“更多文字”,这是真的,因为您正在询问最后一个对象。

现在我的问题是如何获取所有 UITextfield 的内容而不仅仅是标签?

4

2 回答 2

1

分配标签并用它们检索它们。

myTextField.tag = 100;

然后找到它使用这个函数:

myTextField = [view subviewWithTag: 100];
于 2013-01-19T16:43:19.217 回答
1
for (UIView *view in [alertView subviews]){
    if ([view isMemberOfClass:[UITextField class]]){
        UITextField *textField = (UITextField *)view;

        NSLog(@"%@", textField.text);

    }
}
于 2013-01-19T15:35:50.180 回答