UIActionSheet不在视图中显示,屏幕变暗

时间:2010-05-03 15:36:19

标签: iphone

我有一个带标签和导航控制器的应用程序。

除了UIActionSheet之外,一切都很有效。我甚至在同一控制器中显示UIAlertView taht显示正常,但操作表没有显示。屏幕变暗,就像它显示的那样,但没有视图。

以下是相关代码:

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];

注释掉的代码是我尝试过的不同方式。

有关为什么这不起作用的任何想法?

3 个答案:

答案 0 :(得分:2)

[actionSheet showInView:self.view.window];

答案 1 :(得分:0)

不要从当前的keyWindow中显示它 - 使用类似

的实际标签栏显示它
[actionSheet showFromTabBar:delegate.tabBarController.tabBar];

答案 2 :(得分:0)

尝试,这非常有效:

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.
}