从AppDelegate启动ViewController

 金健winner 发布于 2022-12-20 20:48

我有一个自定义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;
    }

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

2 个回答
  • 如果您使用故事板,那么您可以使用以下行来推送您的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];
    

    2022-12-20 20:51 回答
  • 问题

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


    为了PUSH从视图控制器的AppDelegate

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

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

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

    2022-12-20 20:52 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有