Skip to content

Commit

Permalink
Add iPad support
Browse files Browse the repository at this point in the history
The view is now also sent to the action selector
Note: UIPopoverController was used as ActionSheet doesn't support
UIPickerView on the iPad
  • Loading branch information
johnnyg committed Jun 15, 2011
1 parent f8b44a3 commit e99b6e4
Show file tree
Hide file tree
Showing 9 changed files with 898 additions and 96 deletions.
12 changes: 9 additions & 3 deletions ActionSheetPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
SEL _action;

UIActionSheet *_actionSheet;
UIPopoverController *_popOverController;
UIPickerView *_pickerView;
UIDatePicker *_datePickerView;
NSInteger _pickerPosition;
Expand All @@ -46,20 +47,25 @@
@property (nonatomic, assign) SEL action;

@property (nonatomic, retain) UIActionSheet *actionSheet;
@property (nonatomic, retain) UIPopoverController *popOverController;
@property (nonatomic, retain) UIPickerView *pickerView;
@property (nonatomic, retain) UIDatePicker *datePickerView;
@property (nonatomic, assign) NSInteger pickerPosition;

@property (nonatomic, readonly) CGSize viewSize;

//no memory management required for convenience methods

//display actionsheet picker inside View, loaded with strings from data, with item selectedIndex selected. On dismissal, [target action:(NSNumber *)selectedIndex] is called
//display actionsheet picker inside View, loaded with strings from data, with item selectedIndex selected. On dismissal, [target action:(NSNumber *)selectedIndex:(id)view] is called
+ (void)displayActionPickerWithView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title;

//display actionsheet datepicker in datePickerMode inside View with selectedDate selected. On dismissal, [target action:(NSDate *)selectedDate] is called
//display actionsheet datepicker in datePickerMode inside View with selectedDate selected. On dismissal, [target action:(NSDate *)selectedDate:(id)view] is called
+ (void)displayActionPickerWithView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title;

- (id)initWithContainingView:(UIView *)aView target:(id)target action:(SEL)action;
- (id)initForDataWithContainingView:(UIView *)aView data:(NSArray *)someData selectedIndex:(NSInteger)index target:(id)target action:(SEL)action title:(NSString *)title;

- (id)initForDataWithContainingView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title;

- (id)initForDateWithContainingView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title;

//implementation
Expand Down
94 changes: 61 additions & 33 deletions ActionSheetPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ @implementation ActionSheetPicker
@synthesize action = _action;

@synthesize actionSheet = _actionSheet;
@synthesize popOverController = _popOverController;
@synthesize pickerView = _pickerView;
@synthesize datePickerView = _datePickerView;
@synthesize pickerPosition = _pickerPosition;

@dynamic viewSize;

#pragma mark -
#pragma mark NSObject

Expand All @@ -37,7 +40,7 @@ + (void)displayActionPickerWithView:(UIView *)aView data:(NSArray *)data selecte
[actionSheetPicker release];
}

+ (void)displayActionPickerWithView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title{
+ (void)displayActionPickerWithView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title {
ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDateWithContainingView:aView datePickerMode:datePickerMode selectedDate:selectedDate target:target action:action title:title];
[actionSheetPicker showActionPicker];
[actionSheetPicker release];
Expand All @@ -52,7 +55,7 @@ - (id)initWithContainingView:(UIView *)aView target:(id)target action:(SEL)actio
return self;
}

- (id)initForDataWithContainingView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title{
- (id)initForDataWithContainingView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title {
if ([self initWithContainingView:aView target:target action:action] != nil) {
self.data = data;
self.selectedIndex = selectedIndex;
Expand All @@ -75,19 +78,22 @@ - (id)initForDateWithContainingView:(UIView *)aView datePickerMode:(UIDatePicker

- (void)showActionPicker {
[self retain];

//spawn actionsheet
_actionSheet = [[UIActionSheet alloc] initWithTitle:[self isViewPortrait]?nil:@"\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

if (nil != self.data)
//create the new view
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewSize.width, 260)] autorelease];

if (nil != self.data) {
//show data picker
[self showDataPicker];
else
[view addSubview:self.pickerView];
} else {
//show date picker
[self showDatePicker];

UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:[self isViewPortrait]?CGRectMake(0, 0, 320, 44):CGRectMake(0, 0, 480, 44)];
[view addSubview:self.datePickerView];
}

CGRect frame = CGRectMake(0, 0, self.viewSize.width, 44);
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:frame];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;

NSMutableArray *barItems = [[NSMutableArray alloc] init];
Expand Down Expand Up @@ -125,74 +131,95 @@ - (void)showActionPicker {
[pickerDateToolbar setItems:barItems animated:YES];
[barItems release];

[self.actionSheet addSubview:pickerDateToolbar];
[view addSubview:pickerDateToolbar];
[pickerDateToolbar release];

[self.actionSheet showInView:self.view];
if ( [self isViewPortrait] )
[self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
else
[self.actionSheet setBounds:CGRectMake(0, 0, 480, 325)];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//spawn popovercontroller
UIViewController *viewController = [[[UIViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController.view = view;
viewController.contentSizeForViewInPopover = viewController.view.frame.size;
_popOverController = [[UIPopoverController alloc] initWithContentViewController:viewController];
[self.popOverController presentPopoverFromRect:self.view.frame inView:self.view.superview?:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
//spawn actionsheet
_actionSheet = [[UIActionSheet alloc] initWithTitle:[self isViewPortrait]?nil:@"\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[self.actionSheet addSubview:view];
[self.actionSheet showInView:self.view];
self.actionSheet.bounds = CGRectMake(0, 0, self.viewSize.width, self.viewSize.height+5);
}
}

- (void)showDataPicker {
//spawn pickerview
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
CGRect pickerFrame = CGRectMake(0, 40, self.viewSize.width, 216);
_pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];

self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.showsSelectionIndicator = YES;
[self.pickerView selectRow:self.selectedIndex inComponent:0 animated:NO];

[self.actionSheet addSubview:self.pickerView];
}

- (void)showDatePicker {
//spawn datepickerview
CGRect datePickerFrame = CGRectMake(0, 40, 0, 0);
CGRect datePickerFrame = CGRectMake(0, 40, self.viewSize.width, 216);
_datePickerView = [[UIDatePicker alloc] initWithFrame:datePickerFrame];
self.datePickerView.datePickerMode = self.datePickerMode;

[self.datePickerView setDate:self.selectedDate animated:NO];
[self.datePickerView addTarget:self action:@selector(eventForDatePicker:) forControlEvents:UIControlEventValueChanged];

[self.actionSheet addSubview:self.datePickerView];
}

- (void)actionPickerDone {

[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
if (self.actionSheet) {
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
} else {
[self.popOverController dismissPopoverAnimated:YES];
}

if (nil != self.data) {
//send data picker message
[self.target performSelector:self.action withObject:[NSNumber numberWithInt:self.selectedIndex]];
[self.target performSelector:self.action withObject:[NSNumber numberWithInt:self.selectedIndex] withObject:self.view];
} else {
//send date picker message
[self.target performSelector:self.action withObject:self.selectedDate];
[self.target performSelector:self.action withObject:self.selectedDate withObject:self.view];
}

[self release];
}

- (void)actionPickerCancel {
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
if (self.actionSheet) {
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
} else {
[self.popOverController dismissPopoverAnimated:YES];
}
[self release];
}

- (BOOL) isViewPortrait {
return UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
}

- (CGSize) viewSize {
CGSize size = CGSizeMake(320, 480);
if (![self isViewPortrait]) {
size = CGSizeMake(480, 320);
}
return size;
}

#pragma mark -
#pragma mark Callbacks

- (void)eventForDatePicker:(id)sender {
UIDatePicker *datePicker = (UIDatePicker *)sender;

self.selectedDate = datePicker.date;
}

#pragma mark -
#pragma mark UIPickerViewDelegate

Expand Down Expand Up @@ -220,9 +247,10 @@ - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row f


- (void)dealloc {
// NSLog(@"ActionSheet Dealloc");
// NSLog(@"ActionSheet Dealloc");
self.actionSheet = nil;

self.popOverController = nil;

self.pickerView.delegate = nil;
self.pickerView.dataSource = nil;
self.pickerView = nil;
Expand All @@ -231,7 +259,7 @@ - (void)dealloc {
self.datePickerView = nil;
self.selectedDate = nil;

[super dealloc];
[super dealloc];
}

@end
4 changes: 4 additions & 0 deletions Sample/ActionSheetPicker-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@
<true/>
<key>NSMainNibFile</key>
<string>MainWindow</string>
<key>NSMainNibFile~ipad</key>
<string>MainWindow-iPad</string>
<key>UISupportedInterfaceOrientations</key>
<array/>
</dict>
</plist>
20 changes: 20 additions & 0 deletions Sample/ActionSheetPicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
2899E5220DE3E06400AC0155 /* ActionSheetPickerViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2899E5210DE3E06400AC0155 /* ActionSheetPickerViewController.xib */; };
28AD733F0D9D9553002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD733E0D9D9553002E5188 /* MainWindow.xib */; };
28D7ACF80DDB3853001CB0EB /* ActionSheetPickerViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 28D7ACF70DDB3853001CB0EB /* ActionSheetPickerViewController.m */; };
52CFA62313A4D6FA0022667F /* MainWindow-iPad.xib in Resources */ = {isa = PBXBuildFile; fileRef = 52CFA62213A4D6FA0022667F /* MainWindow-iPad.xib */; };
52FDE22813A51D2600D8D2C0 /* ActionSheetPickerViewController-iPad.xib in Resources */ = {isa = PBXBuildFile; fileRef = 52FDE22713A51D2600D8D2C0 /* ActionSheetPickerViewController-iPad.xib */; };
8B2B30A9130EF13700B90F79 /* ActionSheetPicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B2B30A8130EF13700B90F79 /* ActionSheetPicker.m */; };
/* End PBXBuildFile section */

Expand All @@ -31,6 +33,8 @@
28D7ACF70DDB3853001CB0EB /* ActionSheetPickerViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ActionSheetPickerViewController.m; sourceTree = "<group>"; };
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
32CA4F630368D1EE00C91783 /* ActionSheetPicker_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ActionSheetPicker_Prefix.pch; sourceTree = "<group>"; };
52CFA62213A4D6FA0022667F /* MainWindow-iPad.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = "MainWindow-iPad.xib"; path = "iPad/MainWindow-iPad.xib"; sourceTree = "<group>"; };
52FDE22713A51D2600D8D2C0 /* ActionSheetPickerViewController-iPad.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = "ActionSheetPickerViewController-iPad.xib"; sourceTree = "<group>"; };
8B2B30A7130EF13700B90F79 /* ActionSheetPicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ActionSheetPicker.h; path = ../ActionSheetPicker.h; sourceTree = SOURCE_ROOT; };
8B2B30A8130EF13700B90F79 /* ActionSheetPicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ActionSheetPicker.m; path = ../ActionSheetPicker.m; sourceTree = SOURCE_ROOT; };
8D1107310486CEB800E47090 /* ActionSheetPicker-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "ActionSheetPicker-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -77,6 +81,7 @@
080E96DDFE201D6D7F000001 /* Classes */,
29B97315FDCFA39411CA2CEA /* Other Sources */,
29B97317FDCFA39411CA2CEA /* Resources */,
52CFA62113A4D6F90022667F /* iPad */,
29B97323FDCFA39411CA2CEA /* Frameworks */,
19C28FACFE9D520D11CA2CBB /* Products */,
);
Expand Down Expand Up @@ -112,6 +117,15 @@
name = Frameworks;
sourceTree = "<group>";
};
52CFA62113A4D6F90022667F /* iPad */ = {
isa = PBXGroup;
children = (
52FDE22713A51D2600D8D2C0 /* ActionSheetPickerViewController-iPad.xib */,
52CFA62213A4D6FA0022667F /* MainWindow-iPad.xib */,
);
name = iPad;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -163,6 +177,8 @@
files = (
28AD733F0D9D9553002E5188 /* MainWindow.xib in Resources */,
2899E5220DE3E06400AC0155 /* ActionSheetPickerViewController.xib in Resources */,
52CFA62313A4D6FA0022667F /* MainWindow-iPad.xib in Resources */,
52FDE22813A51D2600D8D2C0 /* ActionSheetPickerViewController-iPad.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -193,8 +209,10 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = ActionSheetPicker_Prefix.pch;
INFOPLIST_FILE = "ActionSheetPicker-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
PRODUCT_NAME = ActionSheetPicker;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
Expand All @@ -206,8 +224,10 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = ActionSheetPicker_Prefix.pch;
INFOPLIST_FILE = "ActionSheetPicker-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
PRODUCT_NAME = ActionSheetPicker;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand Down
Loading

0 comments on commit e99b6e4

Please sign in to comment.