applicationWillEnterForeground present a view controller

本秂侑毒 提交于 2019-12-13 19:31:10

问题


I have a PIN view controller which needs to be presented every time the - (void)applicationWillEnterForeground:(UIApplication *)application fires. I have multiple view controllers and when the app enters background the user could be on any of them.

The problem is I don't know how to present the PIN view controller over any view controller that is currently active. Here's how my implementation looks:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    ResourceSingleton *resource = [ResourceSingleton sharedSingleton];
    if ([resource checkIfPINIsEnabled])
    {
        PinViewController *pinView = [[PinViewController alloc] initWithMode:kPINViewControllerModeEnter];
        pinView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self.window.rootViewController presentViewController:pinView animated:YES completion:NULL];
    }
}

But the PIN view appears only if I'm at the first view controller (the root one). How to pop it up on any view controller?

I have seen Using applicationwillenterforeground for a passcode screen but there has to be a better way or am I wrong? This will be for iOS 7 so if only 7 has such a functionality its ok but I am pretty sure it can be done on 6 as well.


回答1:


You could have the app delegate handle the logic for the PIN view, and have that be a view, rather than a view controller. Just add the view as a subview of the window, and it will be shown over anything else.

- (void)applicationWillEnterForeground:(UIApplication *)application {
    UINib *pinNib = [UINib nibWithNibName:@"PINView" bundle:nil];
    UIView *pinView = [pinNib instantiateWithOwner:self options:nil][0];
    [self.window addSubview:pinView];
}

If you make the app delegate the File's Owner of the xib, then you can hook up any outlets you need in the view to the app delegate.




回答2:


If your root view controller is a NavigationController, then pushing or presenting should work in most cases. You already have all the code in place, just create a navigation controller. The only case this would not work is if there is a modal view controller already presented. In that case that needs to be dismissed first.

Here is a little messy implementation that takes care of this case too.

AKPresentedViewController *pres = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"pres"];

UINavigationController *navi = ((UINavigationController*)self.window.rootViewController);
if (navi.presentedViewController) {
    [navi.presentedViewController dismissViewControllerAnimated:YES completion:^{
        [navi presentViewController:pres animated:NO completion:nil];
        }];
} else {
    [navi presentViewController:pres animated:NO completion:nil];
}



回答3:


You could present your PIN view controller the way they mention here.

And for popping the PIN view controller I'm guessing that the user has to enter the correct PIN so the PIN view controller goes away. In that case it can pop itself:

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

Hope this helps!




回答4:


Convert to the objective-C codes to Swift 4, put the codes into func applicationWillEnterForeground

   let pres = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "StartVC")
    let navi: UINavigationController = self.window?.rootViewController as! UINavigationController
    if ((navi.presentedViewController) != nil) {
        navi.dismiss(animated: true) {
            navi.present(pres!, animated: false, completion: nil)
        }
    } else {
        navi.present(pres!, animated: false, completion: nil)
    }


来源:https://stackoverflow.com/questions/18539998/applicationwillenterforeground-present-a-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!