views:

3622

answers:

5

I'd like to popup a simple dialog with an editor box, to let user enter some value then just return. I am wondering whether iPhone SDK has that kind of support.

Thanks.

+1  A: 

No, the SDK does not provide an easy way to do this.

To get functionality similar to the App Store asking for your password (I assume this is what you're after?), you have to roll your own code that provides a similar experience.

Mike McMaster
+4  A: 

You need to create your popup (UIAlertView) and add a UITextField (or whatever component you'd like to use) to it as a sub-view. The UIAlertView won't auto-resize for the component you add so you have to hack that part of it together by adding text to it. The text will increase the height of your popup and provided you don't add too much be hidden by your component.

Max Stewart
+6  A: 

I found a solution at : http://www.iphonedevsdk.com/forum/iphone-sdk-development/1704-uitextfield-inside-uialertview.html

Here is the code which works for me.

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Your title here" message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];

CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
[myAlertView setTransform:myTransform];

[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:myTextField];
[myAlertView show];
[myAlertView release];
BlueDolphin
+1  A: 

Add this function to your code it is called before the Alert is displayed.

  • (void)willPresentAlertView:(UIAlertView *)alertView { alertView.frame = CGRectMake( x, y, width, heigth ); }
Milen Milkovski
A: 

A tip for resizing your UIAlertView...

As Milen Milkovski wrote above, if you set a delegate for the UIAlertView:

UIAlertView* theAlert = [[UIAlertView alloc] initWithTitle:@"Lah"
                                  message:@"dee dah"
                                                  delegate:self
                                  cancelButtonTitle:nil
                             otherButtonTitles:nil];
//NSLog(@"Pre Show: alert frame x,y: %f,%f, alert frame width,height: %f,%f",  theAlert.frame.origin.x,theAlert.frame.origin.y, theAlert.frame.size.width, theAlert.frame.size.height);
[retrievingListAlert show];

You can then modify the frame of the UIAlertView by defining the following UIAlertView callback (in the delegate class- in this case since we used self, the same class as where the UIAlertView was created):

(void)willPresentAlertView:(UIAlertView *)alertView {
    //NSLog(@"willPresentAlertView: alert frame midx,midy: %f,%f, alert frame width,height: %f,%f",  alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, alertView.frame.size.height);
    alertView.frame = CGRectMake( x, y, width, heigth ); 
}

I've found that setting the frame at other times will not work. It appears the show function modifies the frame, presumably while autosizing to it's contents.