Get the current view controller from the app deleg

2019-01-08 07:25发布

i am new to ios. I need to know the current view controller from app delegate.. i have no idea about this and i don't knowto implement this. i am using this code toimplemnt this but it return null values. I followed this link- Get current view controller from the app delegate (modal is possible) need help.

12条回答
劳资没心,怎么记你
2楼-- · 2019-01-08 07:36

It depends on how you set up your UI. You can possibly get your rootViewController and move through the hierarchy if it is set up in such a way.

UIViewController *vc = self.window.rootViewController;
查看更多
相关推荐>>
3楼-- · 2019-01-08 07:40

Here're some class/static functions in swift that I keep in a Utility class and can help you:

// Returns the most recently presented UIViewController (visible)
class func getCurrentViewController() -> UIViewController? {

    // If the root view is a navigation controller, we can just return the visible ViewController
    if let navigationController = getNavigationController() {

        return navigationController.visibleViewController
    }

    // Otherwise, we must get the root UIViewController and iterate through presented views
    if let rootController = UIApplication.sharedApplication().keyWindow?.rootViewController {

        var currentController: UIViewController! = rootController

        // Each ViewController keeps track of the view it has presented, so we
        // can move from the head to the tail, which will always be the current view
        while( currentController.presentedViewController != nil ) {

            currentController = currentController.presentedViewController
        }
        return currentController
    }
    return nil
}

// Returns the navigation controller if it exists
class func getNavigationController() -> UINavigationController? {

    if let navigationController = UIApplication.sharedApplication().keyWindow?.rootViewController  {

        return navigationController as? UINavigationController
    }
    return nil
}
查看更多
ら.Afraid
4楼-- · 2019-01-08 07:45

Swift version of jjv360's great answer, (I got rid of some redundant returns, and I think Swift is more readable)

func getCurrentViewController(_ vc: UIViewController) -> UIViewController? {
    if let pvc = vc.presentedViewController {
        return getCurrentViewController(pvc)
    }
    else if let svc = vc as? UISplitViewController, svc.viewControllers.count > 0 {
        return getCurrentViewController(svc.viewControllers.last!)
    }
    else if let nc = vc as? UINavigationController, nc.viewControllers.count > 0 {
        return getCurrentViewController(nc.topViewController!)
    }
    else if let tbc = vc as? UITabBarController {
        if let svc = tbc.selectedViewController {
            return getCurrentViewController(svc)
        }
    }
    return vc
}

From you AppDelegate,

    guard let rvc = self.window?.rootViewController else {
        return
    }
    if let vc = getCurrentViewController(rvc) {
        // do your stuff here
    }
查看更多
该账号已被封号
5楼-- · 2019-01-08 07:46

Swift 4.0 Global Alert function:-

1.Make A Separate Util .swift class and give name FCommonUtils.swift :-

import Foundation
import SystemConfiguration

class FCommonUtils{
class func alert(title:String = appName, message:String) -> Void {
    //make alert controller
    let alert = UIAlertController(title: title,message: message,preferredStyle: UIAlertControllerStyle.alert)
    //add okay button
    alert.addAction(UIAlertAction.init(title: "Okay",style: .default,handler: { (action) in }))
    //present it on controller
    if  let vc = UIApplication.shared.keyWindow?.rootViewController{
        vc.present(alert, animated: true, completion: nil)
    }
   }
}

2. Use it anywhere like this in side view controllers or from UI Objects :-

FCommonUtils.alert(message: "Test Message")

This is the simple and easiest way to implement global alert feature inside iOS Apps. Happy Coding!! ;)

查看更多
Melony?
6楼-- · 2019-01-08 07:47

This helped me to find the visible view controller. I searched for existing methods and didn't find any. So I wrote my own custom one.

-(id)getCurrentViewController
{
    id WindowRootVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];

    id currentViewController = [self findTopViewController:WindowRootVC];

    return currentViewController;
}

-(id)findTopViewController:(id)inController
{
    /* if ur using any Customs classes, do like this.
     * Here SlideNavigationController is a subclass of UINavigationController.
     * And ensure you check the custom classes before native controllers , if u have any in your hierarchy.
    if ([inController isKindOfClass:[SlideNavigationController class]])
    {
        return [self findTopViewController:[inController visibleViewController]];
    }
    else */
    if ([inController isKindOfClass:[UITabBarController class]])
    {
        return [self findTopViewController:[inController selectedViewController]];
    }
    else if ([inController isKindOfClass:[UINavigationController class]])
    {
        return [self findTopViewController:[inController visibleViewController]];
    }
    else if ([inController isKindOfClass:[UIViewController class]])
    {
        return inController;
    }
    else
    {
        NSLog(@"Unhandled ViewController class : %@",inController);
        return nil;
    }
}

And sample use :

-(void)someMethod
{
    id currentVC = [self getCurrentViewController];
        if (currentVC)
        {
            NSLog(@"currentVC :%@",currentVC);
        }
}
查看更多
Juvenile、少年°
7楼-- · 2019-01-08 07:49

I get the root controller and then iterate through presented VC's:

 UIViewController *current = [UIApplication sharedApplication].keyWindow.rootViewController;

while (current.presentedViewController) {
    current = current.presentedViewController;
}
//now you can use current, for example to present an alert view controller:
[current presentViewController:alert animated:YES completion:nil];
查看更多
登录 后发表回答