Add TextField or other control to UIActionSheet or UIAlertView
Please help.
Thanks
iPhone, Other OS, iphone development
iPhone, Other OS, iphone development
#import <UIKit/UIKit.h>
@interface PickerActionSheet : UIActionSheet {
UIPickerView *picker;
BOOL layoutDone;
}
@property (nonatomic, retain) UIPickerView *picker;
@end
#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
- (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];
}
Add TextField or other control to UIActionSheet or UIAlertView