Unable to add UITextField to UIAlertView on iOS7...works in iOS 6
Asked Answered
Z

7

64

The code below works on iOS6 (and before) but the UITextField does not display in iOS7...any ideas on how to get a UITextField to display in an UIAlterView in iOS7?

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;

nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setKeyboardType:UIKeyboardTypeNumberPad];
[nameField becomeFirstResponder];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];

[nameField release];

Code run for iOS6 displays this:

enter image description here

same code in iOS7 displays this (notice how UITextField is missing and there is no keyboard):

enter image description here

Zebulun answered 31/8, 2013 at 15:6 Comment(8)
the iOS7 is still beta, what did you expect exactly?Directly
Well...I guess if Apple is going to release iOS7 in mid-September, I expect to be able to work with a talented, bright and like-minded community of developers so that our Apps are not unusable for thousands of existing Apple users. Seems kind of silly that Apple doesn't expect anybody to talk about how to be proactive in keeping their users happy. So are we to pretend iOS7 doesn't exist until it's officially released? NDA's are important, but this seems silly...and even sillier that somebody is taking the time to go through iOS7 questions and ding points from people. Just saying...Zebulun
Apple provides a forum for discussion of iOS7 in which embargoed APIs can be discussed. Discussions on here about unreleased, and possibly not-working software are not terribly useful to others - unless very specifically tagged with version numbersTribble
my 2ct: iOS is released and I ran into this issue tooEicher
unbelievable, had to install whole new osx for ios 7 too, unbelievable!Melancholy
Guess what this issue made its way into the public release of iOS 7 and our app is broken on this front as well. Any app that has used this technique (it's not that uncommon, really) is broken. I'm sorry guys but answering that something is "under NDA, and not useful to others" is the complete wrong approach to maintaining a fruitful community which should be helping each other with real-world issues, which this one absolutely is. Beta or not, these issues exist - so please be constructive about it instead of being dismissive and not adding any value to the question.Quoin
Check out this answer: https://mcmap.net/q/172441/-what-39-s-a-simple-way-to-get-a-text-input-popup-dialog-box-on-an-iphone I go over all the possible ways from iOS 4 up on getting this working.Succoth
Just working as charm https://mcmap.net/q/299839/-unable-to-add-uitextfield-to-uialertview-on-ios7-works-in-ios-6Magocsi
N
178

You can't easily alter the view hierarchy of a UIAlertView in iOS 7. (Nor should you; the documentation specifically tells you not to.) Head over to the developer forums to see a long discussion about it.

One alternative in your case is to set alert.alertViewStyle = UIAlertViewStylePlainTextInput; This will add a text field for you. You can access it in the UIAlertView delegate callback by using UITextField *textField = [alertView textFieldAtIndex:0];.

Ned answered 31/8, 2013 at 15:10 Comment(5)
This works fine for single textfield. In case i want to add two text field to uialertview then what is the solution?Schaffhausen
@Schaffhausen UIAlertViewStyleLoginAndPasswordInput. You may be able to set [alertView textFieldAtIndex:1].secureTextEntry = NO if you don't want to obscure the second box. For anything more complex than this, make your own view.Ned
@AaronBrager, But is it going to be accepted by apple on app review?Barrera
What about 3 text-fields? We use this for the password reset functionality: "Current password, new password, new-password again".Quoin
@Quoin Nope, you'll need to look into making your own view or using a 3rd party replacement. github.com/wimagguc/ios-custom-alertview and github.com/mindbrix/TSAlertView are the two best I know of for your needs.Ned
Z
17

@Aaron Brager had the right solution. Additionally I added a line after his suggestion to default a Numeric Keypad.

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;

dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
[dialog textFieldAtIndex:0].keyboardType = UIKeyboardTypeNumberPad;

CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
Zebulun answered 31/8, 2013 at 20:0 Comment(1)
Following both @Aaron Brager's suggestion AND altering the way the Alert is configured... like iTrout's did the trick for me! Thanks to both of you!!!Carbrey
P
6
UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Credit Card Number"
                                  message:@"Please enter your credit card number:"
                                  delegate:self
                                  cancelButtonTitle:@"Cancel"
                                  otherButtonTitles:@"Ok", nil];
        [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        /* Display a numerical keypad for this text field */
        UITextField *textField = [alertView textFieldAtIndex:0];
        textField.keyboardType = UIKeyboardTypeNumberPad;

[alertView show];
Piquet answered 9/1, 2014 at 12:28 Comment(0)
M
1

Just working as charm

Two UITextField in UIAlertView for all version of iOS

-(IBAction) showAlertView {

    UIAlertView *alert;  
    UITextField *callForwardNumber;
    UItextField *callForwardCondition;

    alert = [[UIAlertView alloc] initWithTitle:@"Enter Phone Number & Rule"
                                   message:@""
                                  delegate:self
                         cancelButtonTitle:@"Cancel"
                         otherButtonTitles:@"Save", nil];

    //alert.transform = CGAffineTransformMakeTranslation(0, 110);

    callForwardNumber = [[UITextField alloc] init];
    callForwardNumber.keyboardType = UIKeyboardTypeNumberPad;
    callForwardNumber.text = [R.prefs objectForKey:@"fwd_number"];
    callForwardNumber.borderStyle = UITextBorderStyleRoundedRect;
    callForwardNumber.delegate = self;
    callForwardNumber.tag = 1;

    callForwardCondition = [[UITextField alloc] init];
    callForwardCondition.text = callCondition;
    callForwardCondition.borderStyle = UITextBorderStyleRoundedRect;
    callForwardCondition.delegate = self;
    callForwardCondition.tag = 2;
    [callForwardCondition setKeyboardType:UIKeyboardTypeNumberPad];

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    UIView* customAccessory = 
                   [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 55)];
    callForwardNumber.frame = CGRectMake(0, 0, 245.0, 25.0);
    callForwardCondition.frame = CGRectMake(0, 30.0, 245.0, 25.0);
    [customAccessory addSubview:callForwardNumber];
    [customAccessory addSubview:callForwardCondition];
    [alert setValue:customAccessory forKey:@"accessoryView"];
    [alert show];
} else {
    alert.message = @"\n\n\n";
    [alert show];
    callForwardNumber.frame = CGRectMake(20.0, 45.0, 245.0, 25.0);
    callForwardCondition.frame = CGRectMake(20.0, 75.0, 245.0, 25.0);
    [alert addSubview:callForwardNumber];
    [alert addSubview:callForwardCondition];
}

}
Magocsi answered 7/8, 2014 at 6:46 Comment(0)
S
0

1) In method - (id)initWithAlertTitle:(NSString *)title checkForPassword:(NSString *)password
you should add

self.alertViewStyle = UIAlertViewStylePlainTextInput;

sample:

(id)initWithAlertTitle:(NSString *)title
        checkForPassword:(NSString *)password{
     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
     {
    self.alertViewStyle = UIAlertViewStylePlainTextInput;
    }
    self = [super initWithTitle:title
                        message:@"" // password field will go here
                       delegate:self
              cancelButtonTitle:@"Cancel"
              otherButtonTitles:@"Enter", nil];
    if (self) {
        self.password = password;
        self.hashTechnique = HashTechniqueNone; // use no hashing by default
        secondMessage = @"Please Enter New Password";
        thirdMessage = @"Please Re-Enter Password";
        secondMessageNew = @"Please Enter Password";
    }

NSLog(@" _password_ %@",_password);
NSLog(@"_old_password_ %@",[[NSUserDefaults standardUserDefaults] objectForKey:kPassword]);

return self;
}

in method show add next

(void)show {

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
     {

       UITextField *passwordField = [self textFieldAtIndex:0];
       passwordField.delegate = self;
      self.passwordField = passwordField;
    }
   else
   {
               UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(14, 45, 256, 25)];
               passwordField.secureTextEntry = YES;
               passwordField.placeholder = @"";
               passwordField.backgroundColor = [UIColor whiteColor];



               // Pad out the left side of the view to properly inset the text
               UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 6, 19)];
                passwordField.leftView = paddingView;
                passwordField.leftViewMode = UITextFieldViewModeAlways;

           //    // Set delegate
           self.passwordField.delegate = self;

           // Set as property

            self.passwordField = passwordField;
           // Add to subview
           [self addSubview:_passwordField];
     }

    // Show alert
   [super show];

}

also make changes in method click

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {


    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        UITextField *passwordField = [self textFieldAtIndex:0];
        self.passwordField = passwordField;
    }

    if (buttonIndex == alertView.firstOtherButtonIndex) {

        if ([self enteredTextIsCorrect] || [self.title isEqualToString:secondMessage] || [self.title isEqualToString:secondMessageNew]) {

            if (([self.title isEqualToString:secondMessage] || [self.title isEqualToString:secondMessageNew]) && (self.passwordField.text.length > 0)) {
                self.password = self.passwordField.text;
                self.title = thirdMessage;
                self.passwordField.text = @"";

                if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
                {
                    if ([self.passwordDelegate respondsToSelector:@selector(notifyParent::)]) {
                        [self.passwordDelegate notifyParent:thirdMessage:self.password];
                    }
               }
            }else
            {
                if ([self.title isEqualToString:thirdMessage]) {
                    [[NSUserDefaults standardUserDefaults] setObject:self.password forKey:kPassword];
                    [[NSUserDefaults standardUserDefaults] synchronize];

                    if (self.passwordDelegate) {
                        if ([self.passwordDelegate respondsToSelector:@selector(notifyParentWithState:)]) {
                            [self.passwordDelegate notifyParentWithState:YES];
                        }
                    }
                }else{
                    if ([self.title isEqualToString:secondMessageNew]) {
                        self.title = secondMessageNew;
                    }
                    else{
                        self.title = secondMessage;
                    }

                     self.passwordField.text = @"";
                     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
                     {
                         if ([self.passwordDelegate respondsToSelector:@selector(notifyParent::)]) {
                             [self.passwordDelegate notifyParent:self.title:self.password];
                        }
                    }
                }
            }
        }

        // If incorrect then animate
        else {
            [self animateIncorrectPassword];
        }
    }
}
Seaquake answered 16/11, 2013 at 16:19 Comment(1)
My solution works on iOS 5 and up (you're only using it on iOS 7 and up). There's no need for this hackery unless you're supporting iOS 4. Also, whatever app you copied this from is saving a password in NSUserDefaults, which is a terrible idea.Ned
E
0

You can also give a look to the custom control on cocoacontrols.com. Have a look at MLAertView(ios 7 like UI) and TSAlertView(ios 6 like UI). They can be transformed to a rotation angle as well.

Eggett answered 5/3, 2014 at 20:15 Comment(0)
V
0

I am also facing same issue, During surfing I got the answer it work for me. I hope it also work for you.

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Folder Name?" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.tag = 2;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex    {
    UITextField * alertTextField = [alertView textFieldAtIndex:0];
    NSLog(@"alerttextfiled - %@",alertTextField.text);
}
Verbatim answered 17/11, 2014 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.