You are on page 1of 5

Stack Overflow is a question and answer site for professional and enthusiast programmers.

It's 100% free, no registration Tell me more ×


required.

Unable to add UITextField to UIAlertView on iOS7…works in iOS 6

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
!!!!!!!!!!!!!!!![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:

same code in iOS7 displays this (notice how UITextField is missing and there is no keyboard):
iphone objective-c uitextfield uialertview ios7

asked Aug 31 at 15:06


iTrout
536 1 5 14

1 This question appears to be off-topic because it violates Apple's NDA. – jlehr Aug 31 at 16:23

1 the iOS7 is still beta, what did you expect exactly? – holex Aug 31 at 23:50

13 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 Sep 1 at 0:51

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 numbers – marko Sep 3 at 0:07

2 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 Oct 4 at 14:59

show 2 more comments

3 Answers

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

answered Aug 31 at 15:10


Aaron Brager
7,702 1 10 45

+1 and accepted answer! I'll add the code in an answer that worked. – iTrout Aug 31 at 19:56

1 This works fine for single textfield. In case i want to add two text field to uialertview then what is the solution?
– loganathan Sep 5 at 15:09

1 @loganathan 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. – Aaron Brager Sep 5 at 17:56

@AaronBrager make sense :-) – loganathan Sep 5 at 19:15

@AaronBrager, But is it going to be accepted by apple on app review? – Roy Kronenfeld Sep 20 at 15:41

show 2 more comments

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

answered Aug 31 at 20:00


iTrout
536 1 5 14

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!!! – Marc Watson Sep 25 at 10:00

add comment

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!RefEnter!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
!!!!!!!!!!!!!!!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
also make changes in method click

#pragma!mark!f!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]!||

!!!!!!!!!!!!if!(([self.title!isEqualToString:secondMessage]!||![self.title!isEqualToString
!!!!!!!!!!!!!!!!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
!!!!!!!!!!!!!!!!!!!![[NSUserDefaults!standardUserDefaults]!synchronize];

!!!!!!!!!!!!!!!!!!!!if!(self.passwordDelegate)!{
!!!!!!!!!!!!!!!!!!!!!!!!if!([self.passwordDelegate!respondsToSelector:@selector(notifyParentWithState
!!!!!!!!!!!!!!!!!!!!!!!!if!([self.passwordDelegate!respondsToSelector:@selector(notifyParentWithState
!!!!!!!!!!!!!!!!!!!!!!!!!!!![self.passwordDelegate!notifyParentWithState:YES];
edited Nov 16 at 16:40 answered Nov 16 at 16:19
Jk1 user2999739
2,300 4 8 22 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. – Aaron Brager Nov 22 at 16:22

add comment

Not the answer you're looking for? Browse other questions tagged iphone objective-c
uitextfield uialertview ios7 or ask your own question.

You might also like