WangYang
WangYang

Reputation: 499

objective-c, How to add a UITextField on UIAlertView

I want to wait for a username input by alert dialog, how to add UITextField on UIAlertView

Upvotes: 2

Views: 6493

Answers (3)

Abizern
Abizern

Reputation: 150615

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.

Upvotes: 8

Ajay Sharma
Ajay Sharma

Reputation: 4517

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];

Upvotes: 6

Ajay Sharma
Ajay Sharma

Reputation: 4517

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.......

Upvotes: 0

Related Questions