简体   繁体   中英

objective-c, How to add a UITextField on UIAlertView

我想等待通过警告对话框输入用户名,如何在UIAlertView上添加UITextField

This has changed since iOS5. You can now create a UIAlertView with a style.

UIAlertView now has a property - alertViewStyle that you can set to one of the enum values

  • UIAlertViewStyleDefault
  • UIAlertViewStyleSecureTextInput
  • UIAlertViewStylePlainTextInput
  • UIAlertViewStyleLoginAndPasswordInput

Once you've created the alert view, you set it style and display it. After dismissing the alert, you can access the contents of the fields using the textFieldAtIndex: property of the alert view.

Like this :

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Twitter  Login" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sign In", nil];

myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
myTextField.placeholder=@"Enter User Name";
[myTextField becomeFirstResponder];
[myTextField setBackgroundColor:[UIColor whiteColor]];
myTextField.textAlignment=UITextAlignmentCenter;

// myTextField.layer.cornerRadius=5.0; Use this if you have added QuartzCore framework

[myAlertView addSubview:myTextField];
[myAlertView show];
[myAlertView release];

This has been discussed a few times. Here is a good post on it: UIAlertView with text input

Like this:

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Twitter Login" message:@"\\n\\n\\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sign In", nil];

    myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
    myTextField.placeholder=@"Enter User Name";
    [myTextField becomeFirstResponder];
    [myTextField setBackgroundColor:[UIColor whiteColor]];
    myTextField.textAlignment=UITextAlignmentCenter;
    // myTextField.layer.cornerRadius=5.0; Use this if you have added QuartzCore framework
        [myAlertView addSubview:myTextField];
    [myAlertView show];
    [myAlertView release];

Hope this will solve your problem.......

I have created a post in my blog on the topic "How to add UITextField to UIAlertView from XIB". Using XIB to add is easier than pure coding. Please refer to the following link.

http://creiapp.blogspot.com/2011/08/how-to-add-uitextfield-to-uialertview.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM