Launching ViewController from AppDelegate

后端 未结 6 2049
情书的邮戳
情书的邮戳 2021-02-03 09:51

I have a custom URL scheme and i want to open a certain ViewController which is not the root when i go to this URL. I have been able to do that and what remains is

相关标签:
6条回答
  • 2021-02-03 10:30

    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.


    Solution

    To PUSH ViewController from AppDelegate

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

    To POP that ViewController you just presented, you need to use this code

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

    0 讨论(0)
  • 2021-02-03 10:38

    For Swift 3 version:

    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()
    
    0 讨论(0)
  • 2021-02-03 10:42

    If your using storyboard then you could use below lines for pushing your 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];
    
    0 讨论(0)
  • 2021-02-03 10:42

    Write this code in didFinishingWithLaunchingOptions

      SecViewController *Vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"second"];
       [(UINavigationController *)self.window.rootViewController pushViewController:Vc animated:YES];
    
    0 讨论(0)
  • 2021-02-03 10:43

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

    LoginViewController *loginViewController = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
    
    0 讨论(0)
  • 2021-02-03 10:45

    Your AppDelegate.m should look like this:

    #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;
    }
    
    0 讨论(0)
提交回复
热议问题