UIAlertView with Two TextFields and Two Buttons

妖精的绣舞 提交于 2019-11-30 19:19:27

y==400 is about the bottom of a phone screen. I imagine then, the UIAlertView that never expected to either have subviews added or it's frame set, reset's it's frame to be centered. Can you comment out that very last line and see what happens?

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

Just move the show statement below where you set the alert's frame

I use following for the same purpose

-(void)alertView {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@""
                                                 message:@"Enter User & Password"
                                                delegate:self
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"Login", nil];


alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField * alertTextField1 = [alert textFieldAtIndex:0];
alertTextField1.keyboardType = UIKeyboardTypeDefault;
alertTextField1.placeholder = @"User";

UITextField * alertTextField2 = [alert textFieldAtIndex:1];
alertTextField2.keyboardType = UIKeyboardTypeDefault;
alertTextField2.placeholder = @"Password";


[alert show];

}

If you do not use secure character in second text field add

alertTextField2.secureTextEntry=NO;

I use DDAlertPrompt in my applications. It's easy and a drop-in class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!