【问题标题】:Dismiss UIActionSheet from AppDelegate从 AppDelegate 中关闭 UIActionSheet
【发布时间】:2011-10-21 07:55:25
【问题描述】:

在我看来,我有一个可以从 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;
}

【问题讨论】:

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


    【解决方案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 中移除观察者。

    【讨论】:

    • 显示操作表的 VC 不是锁屏 vc。锁屏 VC 是当应用程序从后台恢复时显示的 VC,并加载到任何已经存在的 vc 之上作为密码检查。
    • 没关系。将该代码工作到呈现并引用 actionSheet 的视图控制器中。当您的应用程序退出活动时,它将关闭 actionSheet,并且在您返回时将不再显示。
    • 成功了!这是我放在dealloc中的吗? [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
    • 是的。或者,如果您以后发现需要添加另一个观察者,您不能忘记删除它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多