Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-19-2008, 04:13 PM   #1 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
Default New info on adding text fields to alerts

I was playing around with class-dump last night and I decided to dump the UIKit framework to see what goodies were in there that Apple wasn't telling us about. As it turns out (and it probably comes as no surprise) UIAlertViews have methods for adding text fields to their views without going through all the fuss that I made using transforms and such. I don't remember the methods right off the top of my head, but they're there and they work rather well. I don't know if Apple checks for this kind of stuff when checking apps for the AppStore, but I don't see why it would be that big of a deal since we aren't linking against any of the private frameworks.

As a side note, the UIGlassButtons are still referenced in the framework and with the proper header they can still be used in the simulator. However, the linker gives an error when trying to compile for the iPhoneOS saying that the object isn't found. It would be cool if we could get those back.

Anyway, if anyone wants the methods for adding text fields to their alert views, I can post them when I get home.
myersn024 is offline   Reply With Quote
Old 08-19-2008, 06:58 PM   #2 (permalink)
Physician developer
 
StatCoder's Avatar
 
Join Date: Aug 2008
Location: Austin, TX
Posts: 221
Default

I'd be interested in seeing what this looks like.
StatCoder is offline   Reply With Quote
Old 08-19-2008, 08:24 PM   #3 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
Default

Alright, here's the goods. The first thing that you'll want to do is to alloc your UIAlertView and then initWithTitle:message:delegate:cancelButtonTitle: otherButtonTitles: just like normal. Then with all that good stuff done, you use the method addTextFieldWithValue:label:. The value is for initializing some text into the text field. The label is for setting a placeholder. Here's some example code.

Code:
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Alert title" message:@"alert message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[myAlert addTextFieldWithValue:nil label:@"<place holder>"];
[[myAlert textField] setTextAlignment:UITextAlignmentCenter];
[[myAlert textField] becomeFirstResponder];
[myAlert show];
[myAlert release];
myAlert = nil;
If you don't tell the text field to become first responder before showing the alert view, you'll wind up with two keyboards. That took a little messing with to get things straightened out. Then, to access the value that was entered into the text field when the ok button is clicked, you just do something like this. In this particular example, myString is an iVar that's already been alloc'ed and init'ed. Thus far, I've not figured out a way to make the text field return if the user presses the return button on the keyboard. I've tried all kinds of things and nothing that I've tried works.

Code:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
     switch(buttonIndex) {
         case 0:
              [myString setText:@"Cancel button pressed"];
              break;
         case 1:
              [myString setText:[[alertView textField] text]];
              break;
     }
}
Just so you know, this will compile and you will get some warnings saying that UIAlertView may not respond to the messages, but it will.

Last edited by myersn024; 08-19-2008 at 08:31 PM.
myersn024 is offline   Reply With Quote
Old 08-20-2008, 03:08 AM   #4 (permalink)
Registered Member
 
Stitch's Avatar
 
Join Date: Aug 2008
Posts: 401
Default

Was there anything in there about adding a small table view to an alert?

Like the "select available WiFi network" popup?

I'd like to try and implement that if possible.
Stitch is offline   Reply With Quote
Old 08-20-2008, 09:35 AM   #5 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
Default

No, but there's some undocumented stuff in UIApplication that I think is for that purpose. I haven't messed around with it, though.
myersn024 is offline   Reply With Quote
Old 08-21-2008, 02:07 AM   #6 (permalink)
Registered Member
 
Stitch's Avatar
 
Join Date: Aug 2008
Posts: 401
Default

Thanks, I'll try and take a look over the weekend.

Anyone know what are the chances of Apple approving your app if you use some of these undocumented features?

I'm guessing you'd never get listed in App Store?
Stitch is offline   Reply With Quote
Old 10-03-2008, 07:06 PM   #7 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 1
Default Getting Return Key to Work

I was able to get the return key to work as follows:
  1. Add the UITextFieldDelegate protocol to your controller.
  2. Set the delegate of the UIAlertView's text field to self.
  3. Add the following function:
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[(UIAlertView *)[textField  superview] dismissWithClickedButtonIndex:1 animated:YES];
	
	return NO;
}
This will intercept the return key press and use it to dismiss the alert with the "OK" button pressed.
frankus is offline   Reply With Quote
Old 10-09-2008, 02:51 PM   #8 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 7
Default

Quote:
Originally Posted by myersn024 View Post
Alright, here's the goods. The first thing that you'll want to do is to alloc your UIAlertView and then initWithTitle:message:delegate:cancelButtonTitle: otherButtonTitles: just like normal. Then with all that good stuff done, you use the method addTextFieldWithValue:label:. The value is for initializing some text into the text field. The label is for setting a placeholder. Here's some example code.

Code:
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Alert title" message:@"alert message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[myAlert addTextFieldWithValue:nil label:@"<place holder>"];
[[myAlert textField] setTextAlignment:UITextAlignmentCenter];
[[myAlert textField] becomeFirstResponder];
[myAlert show];
[myAlert release];
myAlert = nil;
If you don't tell the text field to become first responder before showing the alert view, you'll wind up with two keyboards. That took a little messing with to get things straightened out. Then, to access the value that was entered into the text field when the ok button is clicked, you just do something like this. In this particular example, myString is an iVar that's already been alloc'ed and init'ed. Thus far, I've not figured out a way to make the text field return if the user presses the return button on the keyboard. I've tried all kinds of things and nothing that I've tried works.

Code:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
     switch(buttonIndex) {
         case 0:
              [myString setText:@"Cancel button pressed"];
              break;
         case 1:
              [myString setText:[[alertView textField] text]];
              break;
     }
}
Just so you know, this will compile and you will get some warnings saying that UIAlertView may not respond to the messages, but it will.
I found it very usefull, but now in need to get the string in the editField. How can i get it?
Thanks
giacomop81 is offline   Reply With Quote
Old 10-19-2008, 02:07 PM   #9 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 32
Default

Great work!

I've tried to catch the returning value, but the delegate method doesnt get called.
Quote:
I was able to get the return key to work as follows:
Add the UITextFieldDelegate protocol to your controller.
Set the delegate of the UIAlertView's text field to self.
Add the following function:
1. UIViewController <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
2. [[myAlert textField] delegate:self]; ???
3. I added the function

I am sure that Im not setting the delegate correctly. In the debugger, I dont see a delegate item for the textField.
turbolag is offline   Reply With Quote
Old 10-31-2008, 09:04 AM   #10 (permalink)
Registered Member
 
Forsworn's Avatar
 
Join Date: Oct 2008
Location: Germany
Posts: 504
Default

I got it!

You have to add these methods:

Preparation:
Code:
<UITextFieldDelegate> //import the Delegate protocol
- (void) presentSheet; //declare method in header file
Show the alert:
Code:
- (void) presentSheet { 	
	UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"New Alert" message:@"your message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
	[myAlert addTextFieldWithValue:nil label:@"I'm the Placeholder"];
	
	[[myAlert textField] setDelegate:self];
	[[myAlert textField] setTextAlignment:UITextAlignmentCenter];
	[[myAlert textField] becomeFirstResponder];
	[myAlert show];
	[myAlert release];
	myAlert = nil;
}
Get clicked button and string:
Code:
- (void)alertView:(UIAlertView *)alertView 
clickedButtonAtIndex:(NSInteger)buttonIndex 
{ if ([alertView title] == @"New Alert") { //Only for one specific alert
	
	NSString *myString = [[alertView textField]text]; //Get the string
	NSLog(@"User Pressed Button %d and String is: %@", buttonIndex + 1, myString); //Put it on the debugger
	
	if ([[[alertView textField]text]length] <= 0 || buttonIndex ==0) 
		return; //If cancel or 0 length string the string doesn't matter
	
	if (buttonIndex == 1) {
		//Setup an object or do sth. else here
	}
}
}
Done button:
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	NSLog(@"Dismiss Return");
	[self alertView:(UIAlertView *)[textField  superview] clickedButtonAtIndex:1];
	[(UIAlertView *)[textField  superview] dismissWithClickedButtonIndex:1 animated:NO];
	
	return NO;
}

Last edited by Forsworn; 10-31-2008 at 04:56 PM.
Forsworn is offline   Reply With Quote
Old 11-18-2008, 03:10 AM   #11 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 53
Default

Quote:
Originally Posted by myersn024 View Post
If you don't tell the text field to become first responder before showing the alert view, you'll wind up with two keyboards. That took a little messing with to get things straightened out.
I’m still trying to work this one out. I’m telling my text view to become the first responder, but I still get two keyboards. They come up at almost the same time, but they delay and transparency give it away.
rendezvouscp is offline   Reply With Quote
Old 11-18-2008, 07:35 AM   #12 (permalink)
Registered Member
 
Forsworn's Avatar
 
Join Date: Oct 2008
Location: Germany
Posts: 504
Default

Did you use the code I posted before?
It should work fine.

Look at those lines:
Code:
	[[myAlert textField] becomeFirstResponder];
	[myAlert show];
Forsworn is offline   Reply With Quote
Old 11-18-2008, 01:20 PM   #13 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 53
Default

Yeah, I’ve definitely reviewed all of the code. I can access the text field just fine, but setting it as the first responder just doesn’t seem to work out. The double keyboard animation is almost seamless on the device, so I might just stick with it.
rendezvouscp is offline   Reply With Quote
Old 11-18-2008, 01:28 PM   #14 (permalink)
Registered Member
 
Forsworn's Avatar
 
Join Date: Oct 2008
Location: Germany
Posts: 504
Default

Post your code and I'll help you...
Forsworn is offline   Reply With Quote
Old 11-18-2008, 04:14 PM   #15 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 53
Default

This is really strange. When I start it with Leaks, then it works perfectly; no setting of the first responder necessary. Attached is a sample project of the code; note that the keyboard is twice as dark as what it’s supposed to be.

Note that the sample project doesn’t demonstrate the "Leaks fixes it" randomness.
Attached Files
File Type: zip AlertViewWithTextField.zip (15.3 KB, 150 views)
rendezvouscp is offline   Reply With Quote
Old 11-19-2008, 12:40 AM   #16 (permalink)
Registered Member
 
Forsworn's Avatar
 
Join Date: Oct 2008
Location: Germany
Posts: 504
Default

It's working fine for me:
MEGAUPLOAD - The leading online storage and file delivery service
Forsworn is offline   Reply With Quote
Old 11-19-2008, 01:03 AM   #17 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 53
Default

Well, I certainly don’t know what to say. It’s now working perfectly fine on the device (without becoming first responder). I will certainly try and track down what’s going on if it starts to not work again.
rendezvouscp is offline   Reply With Quote
Old 12-21-2008, 03:11 AM   #18 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 30
Default

I want my application to be available in landscape and portrait, but when I'm in landscape and click the button for the UIAlertView, the alertview and keyboard and both portrait. Does anybody know how to make them landscape?

Oh, and when I press a button on the keyboard while STILL in landscape, the pop-up that shows what key you are pressing comes up where the key would be if it was landscape :S.

Last edited by xspyk; 12-21-2008 at 03:13 AM.
xspyk is offline   Reply With Quote
Old 12-21-2008, 06:22 AM   #19 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,049
Default

Quote:
Originally Posted by xspyk View Post
I want my application to be available in landscape and portrait, but when I'm in landscape and click the button for the UIAlertView, the alertview and keyboard and both portrait. Does anybody know how to make them landscape?

Oh, and when I press a button on the keyboard while STILL in landscape, the pop-up that shows what key you are pressing comes up where the key would be if it was landscape :S.
Your question has nothing to do with this thread. Have some respect and don't hijack threads...create your own if you want a response.
wuf810 is offline   Reply With Quote
Old 01-05-2009, 12:04 AM   #20 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 30
Default

Quote:
Originally Posted by wuf810 View Post
Your question has nothing to do with this thread. Have some respect and don't hijack threads...create your own if you want a response.
Yes, my question actually does.
This thread is about adding text fields to alerts.
I have an alert with a text field, and I was wondering if anyone knew how to make it work in landscape.
xspyk is offline   Reply With Quote
Old 01-12-2009, 10:50 AM   #21 (permalink)
FlipConversionDesign
 
Join Date: Aug 2008
Location: Orangeville, Ontario
Posts: 83
Send a message via MSN to flipconversion
Default

Seconded. This would be *great*.
flipconversion is offline   Reply With Quote
Old 01-12-2009, 11:44 AM   #22 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,049
Default

Quote:
Originally Posted by xspyk View Post
Yes, my question actually does.
This thread is about adding text fields to alerts.
I have an alert with a text field, and I was wondering if anyone knew how to make it work in landscape.
OK. You need to specifically force the orientation. try something like this.

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLand scapeRight animated:NO];
wuf810 is offline   Reply With Quote
Old 01-19-2009, 04:36 PM   #23 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 41
Default 2 text fields?

Can anyone specify whether or not you can somehow grab the value of user input in TWO UITextFields added to the alertView (in Succession): ????

Code:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Input New Values" message:@"Enter new values" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Record New Values", nil];
	
[myAlertView addTextFieldWithValue:nil label:@"<Value 1 Placeholder>"];
[myAlertView addTextFieldWithValue:nil label:@"<Value 2 Placeholder>"];

[[myAlertView textField] becomeFirstResponder];
[myAlertView show];
[myAlertView release];
HoofSC is offline   Reply With Quote
Old 07-16-2009, 05:40 AM   #24 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 4
Default

Quote:
Originally Posted by Stitch View Post
Thanks, I'll try and take a look over the weekend.

Anyone know what are the chances of Apple approving your app if you use some of these undocumented features?

I'm guessing you'd never get listed in App Store?
Has anyone found out if apple will approve your app using this stuff? I realise this was posted a while ago, but would like to know if it is worth my time using it
rossmclachlan is offline   Reply With Quote
Old 07-16-2009, 08:18 AM   #25 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 791
Default

Quote:
Originally Posted by HoofSC View Post
Can anyone specify whether or not you can somehow grab the value of user input in TWO UITextFields added to the alertView (in Succession): ????

Code:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Input New Values" message:@"Enter new values" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Record New Values", nil];
	
[myAlertView addTextFieldWithValue:nil label:@"<Value 1 Placeholder>"];
[myAlertView addTextFieldWithValue:nil label:@"<Value 2 Placeholder>"];

[[myAlertView textField] becomeFirstResponder];
[myAlertView show];
[myAlertView release];
Hi, grab each individual text field with [myAlertView textFieldAtIndex:0]
nobre84 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,481
Threads: 89,092
Posts: 380,127
Top Poster: BrianSlick (7,091)
Welcome to our newest member, paverlight
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:39 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0