簡體   English   中英

從AppDelegate啟動ViewController

[英]Launching ViewController from AppDelegate

我有一個自定義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 1開始。


AppDelegate 推送 ViewController

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

彈出您剛才介紹的是視圖控制器,你需要使用這個代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM