从 AppDelegate 中关闭 UIActionSheet

发布于 2024-12-10 19:50:21 字数 3186 浏览 1 评论 0原文

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

In my view, I have an actionsheet that may be presented from a UIBarButton (iPad). If user goes to homescreen and back, app shows a lock screen wehre user must enter password for security. But if the popover wasn't dismised before going into background, it is still showing on top of lockscreen. The UIActionSheet is a property of that VC, not the App Delegate.

The method in my delegate is:

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

Code:

- (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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2024-12-17 19:50:22

这应该做你想做的事。

-(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 中删除观察者。

This should do what you want.

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

you could also use the UIApplicationDidEnterBackgroundNotification, what ever makes sense for you application flow.

remember to remove observer in dealloc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文