ios 从 AppDelegate 启动 ViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25891444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:32:00  来源:igfitidea点击:

Launching ViewController from AppDelegate

iosuiviewcontrollerappdelegate

提问by Elgert

I have a custom URL scheme and i want to open a certain ViewControllerwhich is not the root when i go to this URL. I have been able to do that and what remains is pushing this ViewControllerinto the navigationControllerfrom the AppDelegatewhere i handle my URL like this :

我有一个自定义的 URL 方案,ViewController当我转到这个 URL 时,我想打开一个不是根的特定URL。我已经能够做到这一点,剩下的就是将它从我处理我的 URL的地方推ViewController入,如下所示:navigationControllerAppDelegate

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

but it only launches my app and not the ListingViewControlleri want to launch. Any idea how can i do it differently ?

但它只启动我的应用程序,而不是ListingViewController我想启动的应用程序。知道我该怎么做吗?

采纳答案by E-Riddie

Issue

问题

To deal with pushing and popping the viewControllers from AppDelegate, you need to use [UIApplication sharedApplication]which keeps track to all of viewControllers, beginning with root one.

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



Solution

解决方案

To PUSHViewController from AppDelegate

为了PUSH从视图控制器的AppDelegate

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

To POPthat ViewController you just presented, you need to use this code

POP您刚刚呈现的 ViewController,您需要使用此代码

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


回答by nikhil84

If your using storyboard then you could use below lines for pushing your VC.

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

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

Update:-

更新:-

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

回答by Sakshi Singla

Write this code in didFinishingWithLaunchingOptions

在 didFinishingWithLaunchingOptions 中编写此代码

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

回答by mike vorisis

For Swift 3 version:

对于 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()

回答by n.by.n

Your AppDelegate.m should look like this:

您的 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;
}

回答by jomafer

If you want to push a UIViewController, you probably are forgetting to init it with the NibName:

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

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