Add TextField or other control to UIActionSheet or UIAlertView

Anyone know how to derived UIActionSheet or UIAlertView and add additional UIControl (e.g. TextField ...) to the dialog. It is nice to have example how to do it. I try but still not show the TextField in modal dialog.

Please help.

Thanks

iPhone, Other OS, iphone development

Posted on Aug 21, 2008 8:09 AM

Reply
24 replies

Dec 18, 2008 1:36 PM in response to cheinelt

In response to the earlier post about adding a UIPickerView, here's an example of a UIActionSheet with a picker based on the UITextView example above. The heavy lifting was already done; I just removed unneeded code and hooked in to the UIActionSheet constructor. As usual, the UIPickerView needs a delegate and data source, so I have added the picker as a property to the subclass for easy setting.

PickerActionSheet.h

#import <UIKit/UIKit.h>
@interface PickerActionSheet : UIActionSheet {
UIPickerView *picker;
BOOL layoutDone;
}
@property (nonatomic, retain) UIPickerView *picker;
@end


PickerActionSheet.m

#import "PickerActionSheet.h"
@implementation PickerActionSheet
@synthesize picker;
- (id)initWithTitle:(NSString *)title delegate:(id < UIActionSheetDelegate >)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ... {
self = [super initWithTitle:title delegate:delegate cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitles];
if (self) {
UIPickerView *p = [[UIPickerView alloc] initWithFrame:CGRectZero];
[self addSubview:p];
self.picker = p;
[p release];
}
return self;
}
- (void)dealloc {
[super dealloc];
}
/*
* Determine maximum y-coordinate of UILabel objects. This method assumes that only
* following objects are contained in subview list:
* - UILabel
* - UITextField
* - UIThreePartButton (Private Class)
*/
- (CGFloat) maxLabelYCoordinate {
// Determine maximum y-coordinate of labels
CGFloat maxY = 0;
for( UIView *view in self.subviews ){
if([view isKindOfClass:[UILabel class]]) {
CGRect viewFrame = [view frame];
CGFloat lowerY = viewFrame.origin.y + viewFrame.size.height;
if(lowerY > maxY)
maxY = lowerY;
}
}
return maxY;
}
- (void)layoutSubviews {
[super layoutSubviews];
if(!layoutDone) {
CGRect frame = [self frame];
CGFloat alertWidth = frame.size.width;
CGFloat pickerHeight = self.picker.frame.size.height;
CGFloat labelMaxY = [self maxLabelYCoordinate];

// Insert UIPickerView below labels and move other fields down accordingly
for(UIView *view in self.subviews){
if([view isKindOfClass:[UIPickerView class]]){
CGRect viewFrame = CGRectMake(0, labelMaxY, alertWidth, pickerHeight);
[view setFrame:viewFrame];
} else if(![view isKindOfClass:[UILabel class]]) {
CGRect viewFrame = [view frame];
viewFrame.origin.y += pickerHeight;
[view setFrame:viewFrame];
}
}

// size UIAlertView frame by height of UIPickerView
frame.size.height += pickerHeight + 2.0;
frame.origin.y -= pickerHeight + 2.0;
[self setFrame:frame];
layoutDone = YES;
}
}
@end


usage:

- (IBAction)showPicker:(id)sender {
PickerActionSheet * sheet = [[PickerActionSheet alloc] initWithTitle:@"Pick Something"
delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
sheet.picker.delegate = self;
sheet.picker.dataSource = self;
[sheet showInView:self.view];
[sheet release];
}


Thanks to cheinelt for the bulk of this code!

p.

Message was edited by: pegli

Feb 18, 2009 12:57 PM in response to cheinelt

Hi,

Everytime I try to use this code of yours, my app crashes. The problem occurs when the application returns from reading what was typed, I mean, when the user presses ok on the alert window.

Any idea of what may be causing this?

I have even found another code based on yours here
http://code.google.com/p/itextuploader/source/browse/trunk/TextAlertView.m?spec= svn38&r=38

this code has a last method yours don't have.

Both codes show the same problem - application crashes right after the user types and presses OK.

Any help is appreciated. Thanks.

Message was edited by: Magno Urbano*

-

Apr 16, 2009 10:49 AM in response to Magno Urbano*

Magno,

I was having the same problem. The solution is as follows - the object you designate as the delegate has to remain in existence. When I realized I was autoreleasing the object, and stopped, the problem went away. Make sure (explicitly if necessary) that the object you designate as a delegate does not get released until after the alert is finished with everything that it is supposed to do and (hopefully) that will fix your issues.

Object persistence, the bane of my existence.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Add TextField or other control to UIActionSheet or UIAlertView

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.