UIAlertView with Two TextFields and Two Buttons

后端 未结 5 852
星月不相逢
星月不相逢 2021-01-05 03:32

Problem - I want a UIAlertView which contains a Title, two textfields with placeholders and two buttons. What I have done till now - I have

5条回答
  •  一个人的身影
    2021-01-05 03:53

    Starting from iOS 5 is possible to use the property alertViewStyle of UIAlertView to get different type of alert views, also with text fields.

    Possible values:

    • UIAlertViewStyleDefault
    • UIAlertViewStyleSecureTextInput
    • UIAlertViewStylePlainTextInput
    • UIAlertViewStyleLoginAndPasswordInput

    You can use the UIAlertViewStyleLoginAndPasswordInput to obtain an UIAlertView with two text fields and then you can customize some properties of the textFields:

     UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Your_Title" message:@"Your_message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [av setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
    
    // Alert style customization 
    [[av textFieldAtIndex:1] setSecureTextEntry:NO];
    [[av textFieldAtIndex:0] setPlaceholder:@"First_Placeholder"];
    [[av textFieldAtIndex:1] setPlaceholder:@"Second_Placeholder"];
    [av show];
    

    You can access the values of the text fields on the alertView:clickedButtonAtIndex: of UIAlertViewDelegate.

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
         NSLog(@"1 %@", [alertView textFieldAtIndex:0].text);
         NSLog(@"2 %@", [alertView textFieldAtIndex:1].text);
    }
    

提交回复
热议问题