从AppDelegate中删除UIActionSheet

时间:2011-10-20 22:45:53

标签: iphone objective-c cocoa-touch ipad uiactionsheet

在我看来,我有一个可以从UIBarButton(iPad)呈现的动作表。如果用户进入主屏幕并返回,应用程序会显示锁定屏幕,用户必须输入密码以确保安全。但是如果在进入后台之前没有将其破坏,那么它仍会显示在锁屏上方。 UIActionSheet是该VC的属性,而不是App Delegate。

我的代表中的方法是:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"STATUS - Application did become active");
    [[UIAccelerometer sharedAccelerometer] setDelegate:nil];
    [_notActiveTimer invalidate];
    _notActiveTimer = nil;

    [[NSNotificationCenter defaultCenter] postNotificationName:@"_application_background" object:self userInfo:NULL];

    LockScreen *vc = [[[LockScreen alloc] initWithNibName:@"LockScreen" bundle:nil] autorelease];
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    vc.showFullUsernamePasswordLogin = TRUE;
    [self.splitView presentModalViewController:vc animated: YES];
}

代码:

- (IBAction)showeRXActionSheet:(id)sender
{
    if (self.actionSheet != nil) {
        [self.actionSheet dismissWithClickedButtonIndex:0 animated:NO];
    }

    if (!self.eRXActionSheet)
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"eRX Menu" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"New eRX", @"eRX Refills", nil];
        sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        self.eRXActionSheet = sheet;
        [sheet release];

        [self.eRXActionSheet showFromBarButtonItem:sender animated:YES];

        return;
    }

    [self.eRXActionSheet dismissWithClickedButtonIndex:self.eRXActionSheet.cancelButtonIndex animated:YES];
    self.eRXActionSheet = nil;
}

- (IBAction)actionButtonClicked:(id)sender
{
    if (self.eRXActionSheet != nil) {
        [self.eRXActionSheet dismissWithClickedButtonIndex:0 animated:NO];
    }

    if (!self.actionSheet)
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Action Menu" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Help", @"Lock", @"Log Out", nil];
        sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        self.actionSheet = sheet;
        [sheet release];

        [self.actionSheet showFromBarButtonItem:sender animated:YES];

        return;
    }

    [self.actionSheet dismissWithClickedButtonIndex:self.actionSheet.cancelButtonIndex animated:YES];
    self.actionSheet = nil;
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet == self.eRXActionSheet)
    {
        if (buttonIndex == 0)
        {
            [self erxButtonClicked];
        }
        else if (buttonIndex == 1)
        {
            [self erxRefillButtonClicked];
        }
    }
    else
    {
        if (buttonIndex == 0)
        {
            [self helpButtonClicked];
        }
        else if (buttonIndex == 1)
        {
            [self lockButtonClicked];
        }
        else if (buttonIndex == 2)
        {
            [self logOut];
        }
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    self.eRXActionSheet = nil;
    self.actionSheet = nil;
}

1 个答案:

答案 0 :(得分:1)

这应该做你想要的。

-(void)dismissSheet{
    if (self.actionSheet){
        [self.actionSheet dismissWithClickedButtonIndex:0 animated:NO];
    }
}

-(void)viewDidLoad{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissSheet) name:UIApplicationWillResignActiveNotification object:nil];
        // Your other setup code
}

您也可以使用UIApplicationDidEnterBackgroundNotification,这对您的应用程序流程有意义。

记得在dealloc中删除观察者。