1

Possible Duplicate:
How to display temporary popup message on iPhone/iPad/iOS

I want to be able to pop a small screen/view on top of my current view with a UITextField and two buttons on it. I want to allow the user to enter some data or cancel.

I don't want to use a completely new screen to do this. Is there any nice way to go about achieving this? Is there something provided in the iOS API's to do this?

Any links to tutorial would be appreciated. Have been reading around on google but all I can find are examples of full screen modal views.

Thanks, -Code

Community
  • 1
  • 1
  • Its not a duplicate. Its very frustrating when some idiot comes along and does this. The answer to my question is below. –  Apr 21 '12 at 14:17

2 Answers2

2

you can subclass UIAlertView class to have a textField (or multiple textfields)

here is how your custom class will look like: (AlertPrompt.h)

@interface AlertPrompt : UIAlertView
{
    UITextField *emailTextField;
    UITextField *nameTextField;

}

@property (nonatomic, retain) UITextField *emailTextField;
@property (nonatomic, retain) UITextField *nameTextField;

- (id)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;
@end

AlertPrompt.m

#import "AlertPrompt.h"

@implementation AlertPrompt
@synthesize emailTextField, nameTextField;

- (id)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{

if (self = [super initWithTitle:title message:@" \n\n\n" delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
{
    self.frame = CGRectMake(0, 0, 300, 400);
    UITextField *nameTF = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
    UITextField *emailTF = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80, 260.0, 25.0)];
    [nameTF setBackgroundColor:[UIColor whiteColor]]; 
    [nameTF setAutocorrectionType:UITextAutocorrectionTypeNo];
    [nameTF setPlaceholder:@"name"];
    [emailTF setBackgroundColor:[UIColor whiteColor]]; 
    [emailTF setAutocorrectionType:UITextAutocorrectionTypeNo];
    [emailTF setPlaceholder:@"email"];

    [self addSubview:nameTF];
    [self addSubview:emailTF];
    self.nameTextField = nameTF;
    self.emailTextField = emailTF;

    if (delegate) {
        self.emailTextField.delegate = delegate;
        self.nameTextField.delegate = delegate;
    }

    [nameTF release];
    [emailTF release];

}
return self;
}
- (void)show
{
    [nameTextField becomeFirstResponder];
    [super show];
}

 - (void)dealloc
{
    [nameTextField release];
    [emailTextField release];

    [super dealloc];
}
@end

you'll notice that am passing @" \n\n\n" as parameter in the init method so that makes space for the textFields I want to display.. so you'll have to modify the message param depending on the number of textfields you want to display in the prompt.

Hope this helps

Manish Ahuja
  • 4,509
  • 3
  • 28
  • 35
0

Check out the UIAlertView and UIAlertViewDelegate classes. They should do what you want.

Here's a tutorial that should help!

If you need text input (rather than just buttons) try this tutorial.

Mike Fahy
  • 5,487
  • 4
  • 24
  • 28