从AppDelegate启动ViewController

我有一个自定义URL方案,我想打开一个特定的ViewController ,当我转到这个URL时,它不是根。 我已经能够做到这一点,剩下的就是从AppDelegate将这个ViewController推送到navigationController ,我在这里处理我的URL:

 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { if ([[url scheme] isEqualToString:@"njoftime"]) { NSDictionary *getListingResponse = [[NSDictionary alloc]init]; getListingResponse = [Utils getListing:[url query]];; if ([[getListingResponse objectForKey:@"status"] isEqualToString:@"success"]) { ListingViewController *listingView = [[ListingViewController alloc]init]; [self.window.rootViewController.navigationController pushViewController:listingView animated:YES]; return YES; } 

但它只启动我的应用程序,而不是我想要启动的ListingViewController 。 知道我怎么能以不同的方式做到这一点?

问题

要处理从AppDelegate推送和弹出viewControllers,您需要使用[UIApplication sharedApplication]跟踪所有viewControllers,从root用户开始。


AppDelegate 推送 ViewController

 ListingViewController *listingVC = [[ListingViewController alloc] init]; [(UINavigationController *)self.window.rootViewController pushViewController:listingVC animated:YES]; 

要弹出您刚刚展示的ViewController,您需要使用此代码

 [(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController popViewControllerAnimated:YES ]; 

如果您使用故事板,那么您可以使用以下行来推送您的VC。

  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; YOURCLASS *obj=[storyboard instantiateViewControllerWithIdentifier:@"YOUR_CLASS_STORYBOARD_ID"]; [self.window.rootViewController.navigationController pushViewController:obj animated:YES]; 

更新: –

 ListingViewController *listingVC = [[ListingViewController alloc] init]; UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:listingVC]; [self.window.rootViewController presentViewController:navCon animated:YES completion:nil]; 

在didFinishingWithLaunchingOptions中编写此代码

  SecViewController *Vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"second"]; [(UINavigationController *)self.window.rootViewController pushViewController:Vc animated:YES]; 

对于Swift 3版本:

 let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let initialViewControlleripad : UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "initial") as! UINavigationController self.window = UIWindow(frame: UIScreen.main.bounds) self.window?.rootViewController = initialViewControlleripad self.window?.makeKeyAndVisible() 

如果你想推送一个UIViewController,你可能忘了使用NibName初始化它:

 LoginViewController *loginViewController = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil]; 

您的AppDelegate.m应如下所示:

 #import "TestViewController.h" @interface AppDelegate () @property (strong, nonatomic) TestViewController *viewController; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.viewController = [[TestViewController alloc]init]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; }