Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone alertview with textfield

I have an UIAlertView with a UITextField in it. I want to type the mail id and submit in UIAlertView's ok button, but UITextField in the UIAlertView have no response, Please help me.

thankz

like image 630
Nipin P N Avatar asked Apr 07 '11 04:04

Nipin P N


People also ask

How do you add a TextField to an alert?

We can have a text field in an alert by using TextField or SecureField as action content. We add text field in actions , not message . Text("Please enter your username and password.") TextField and SecureField are supported in iOS 16.

What is TextField in iOS?

A control that displays an editable text interface. iOS 13.0+ iPadOS 13.0+ macOS 10.15+ Mac Catalyst 13.0+ tvOS 13.0+ watchOS 6.0+


2 Answers

From iOS 5 the approach above is no longer necessary. Just set the alertViewStyle property to the appropriate style (UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, or UIAlertViewStyleLoginAndPasswordInput).

Example:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Email" message:@"Enter your email:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];

and you can have the response back as

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UITextField *emailTextField = [alertView textFieldAtIndex:0];
    NSLog(@"%@",emailTextField.text);
}
like image 56
xcoder Avatar answered Oct 06 '22 00:10

xcoder


UIAlertView with UITextField:

Usage:

NSString *title         = NSLocalizedString(@"Your Title","");
NSString *placeholder   = NSLocalizedString(@"Placeholder Text","");
NSString *message       = NSLocalizedString(@"A message to the user.","");
NSString *cancel        = NSLocalizedString(@"Cancel","");
NSString *okay          = NSLocalizedString(@"Continue","");

prompt = [[AlertPrompt alloc] initWithTitle:title 
                   placeholder:placeholder 
                       message:message 
                      delegate:self 
             cancelButtonTitle:cancel 
                 okButtonTitle:okay];
[prompt show];
[prompt release];

.h

#import <Foundation/Foundation.h>

@interface AlertPrompt : UIAlertView <UITextFieldDelegate>
{
    UITextField *textField;
    NSString *enteredText;
}
@property(nonatomic,retain) UITextField *textField;
@property (nonatomic, retain, readonly) NSString *enteredText;
- (id)initWithTitle:(NSString *)title placeholder:(NSString *)placeholder message:(NSString *)message delegate:(id)delegate 
  cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle;
@end

.m

#import ".h"

@implementation AlertPrompt

@synthesize textField;
@synthesize enteredText;

#pragma mark -
#pragma mark AlertPrompt Delegates

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

    if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
    {
        self.title      = title;
        self.message      = [NSString stringWithFormat:@"%@\n\n\n",message];
        self.delegate    = delegate;

        UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 75.0f, 260.0, 30.0)]; 
        [theTextField setBackgroundColor:[UIColor clearColor]]; 
        theTextField.borderStyle = UITextBorderStyleRoundedRect;
        theTextField.textColor = [UIColor blackColor];
        theTextField.font = [UIFont systemFontOfSize:18.0];
        theTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
        theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
        theTextField.returnKeyType = UIReturnKeyDone;
        theTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        theTextField.placeholder = placeholder;
        theTextField.delegate = self;
        [self insertSubview:theTextField atIndex:0];
        self.textField = theTextField;
        [theTextField release];
        CGAffineTransform translate = CGAffineTransformMakeTranslation(0, -10); 
        [self setTransform:translate];
    }
    return self;
}

- (void)show{
    [textField becomeFirstResponder];
    [super show];
}

- (NSString *)enteredText{
    return textField.text;
}

- (void)dealloc{
    [textField resignFirstResponder];
    [textField release];
    [super dealloc];
}
@end

Delegate:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if([alertView.title isEqualToString:@"Your Title"])
    {    
        if(buttonIndex == 1)
        {
            /*  get the user iputted text  */
            NSString *inputValue = [(AlertPrompt *)alertView enteredText];
            NSLog(@"User Input: %@",inputValue);
        {
    }
}
like image 33
WrightsCS Avatar answered Oct 06 '22 00:10

WrightsCS