64
votes

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

7
the iOS7 is still beta, what did you expect exactly?holex
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...iTrout
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 numbersmarko
unbelievable, had to install whole new osx for ios 7 too, unbelievable!colin lamarre
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.Marchy

7 Answers

178
votes

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

17
votes

@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];
6
votes
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];
1
votes

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

}
0
votes

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];
        }
    }
}
0
votes

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.

0
votes

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);
}