从AppDelegate启动ViewController

Elg*_*ert 12 uiviewcontroller ios appdelegate

我有一个自定义URL方案,ViewController当我转到此URL时,我想打开一个不是根的某个.我已经能够做到这一点,剩下的是推动这一ViewControllernavigationControllerAppDelegate那里我处理我的网址是这样的:

- (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;
    }
Run Code Online (Sandbox Code Playgroud)

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

E-R*_*die 12

问题

要处理从AppDelegate推送和弹出viewControllers ,你需要使用[UIApplication sharedApplication]它跟踪所有viewControllers,从root 1开始.


为了PUSH从视图控制器的AppDelegate

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

POP您刚才介绍的是视图控制器,你需要使用这个代码

[(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController popViewControllerAnimated:YES ];
Run Code Online (Sandbox Code Playgroud)


nik*_*l84 7

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

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
 YOURCLASS *obj=[storyboard instantiateViewControllerWithIdentifier:@"YOUR_CLASS_STORYBOARD_ID"];
[self.window.rootViewController.navigationController pushViewController:obj animated:YES];
Run Code Online (Sandbox Code Playgroud)

更新: -

ListingViewController *listingVC = [[ListingViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:listingVC];
[self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)