I have an app with tabs and navigation controllers.
Everything works great except a UIActionSheet. I even have a UIAlertView taht shows fine in the same controller, but the action sheet doesn't show. The screen goes dark, like it's showing, but no view.
Here's the relevant code:
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Erase the file?"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Clear List"
otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
//[actionSheet showInView:self.view];
//MyAppDelegate* delegate = [[UIApplication sharedApplication] delegate];
//[actionSheet showInView:delegate.tabBarController.view];
[actionSheet release];
The commented out code was the different ways of showing it that I've tried.
Any thoughts as 开发者_开发知识库to why this isn't working?
[actionSheet showInView:self.view.window];
Don't show it from the current keyWindow - show it from the actual tab bar with something like
[actionSheet showFromTabBar:delegate.tabBarController.tabBar];
Try, this works perfectly:
UITextField *tempTextField;
UIActionSheet *myActionSheet;
- (IBAction) myDatePickerAction:(id)sender {
tempTextField = (UITextField *)sender;
[(UITextField *)sender resignFirstResponder];
myActionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[myActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
UIDatePicker *theDatePicker = [[UIDatePicker alloc] init];
theDatePicker.datePickerMode = UIDatePickerModeDate;
CGRect datePickRect = theDatePicker.frame;
datePickRect.size.height = 120;
theDatePicker.frame = datePickRect;
[theDatePicker addTarget:self action:@selector(setDateValue:) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:theDatePicker];
[theDatePicker release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES;
CGRect closeButtonRect = closeButton.frame;
closeButtonRect.size.width = 50;
closeButtonRect.size.height = 30;
closeButton.frame = closeButtonRect;
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:closeButton];
[closeButton release];
[myActionSheet showFromTabBar:appDelegate.tabBarController.tabBar];
CGRect actionSheetRect = myActionSheet.bounds;
actionSheetRect.size.height = 300;
myActionSheet.bounds = actionSheetRect;
}
- (IBAction)setDateValue:(id)sender {
NSDateFormatter *myDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[myDateFormatter setDateFormat:@"MM/dd/yyyy"];
NSLog(@"Formatted date1:%@",[myDateFormatter stringFromDate:[(UIDatePicker *)sender date]]);
tempTextField.text = [myDateFormatter stringFromDate:[(UIDatePicker *)sender date]];
tempTextField = nil;
}
- (IBAction)dismissActionSheet:(id)sender {
[myActionSheet dismissWithClickedButtonIndex:0 animated:YES];
myActionSheet = nil;
//[myActionSheet release];
//Do not release here. Just set to nil and release it in dealloc.
}
精彩评论