-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 つの Textfields と 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 に答える