美文网首页
两个UINavigationController 如何相互切换

两个UINavigationController 如何相互切换

作者: 百有异用是書生 | 来源:发表于2016-11-19 17:39 被阅读0次

独立开发项目中,由于开发经验有限,项目的结构有些混乱,遇到很多坑,自己一个个的去解决,但是我感觉解决的方式方法肯定不是很好,主要身边没有iOS的前辈能够探讨,导致自己进步感觉有点慢。。。网络上虽然也能交流,不过我好像工作之余,看帖和文档是我主要学习方式,所以,提出的观点可能存在一些问题,或者并不是最简单或者最合理的方式,在此诚恳的希望同学们,前辈们能够多多指点。

一、此文章的产生原因

  • 1、项目登陆注册界面与内容界面的UINavigationController最开始差别很大(颜色,样式)。

  • 2、首次登陆需要登陆或者注册才能进入内容界面,一周内打开app则不需要登陆直接进入内容界面。

  • 3、在内容界面里要可以退出到登陆注册界面
    so ----登陆注册部分是一个单独的UINavigationController

----内容界面是一个独立的UINavigationController管理内容的所有界面

二、看图说话

在didFinishLaunching中写如下伪代码,是否第一次登陆根据具体情况看官自行添加,ViewController是登陆注册界面,TwoViewController是内容界面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

if (第一次登陆) {
        ViewController *vc=[[ViewController alloc]init];
        self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _nv=[[UINavigationController alloc]initWithRootViewController:vc];
        self.window.backgroundColor=[UIColor whiteColor];
        self.window.rootViewController=_nv;
        [self.window makeKeyAndVisible];
        return YES;
    }
    else if (6天内登陆) {
        TwoViewController *vc=[[TwoViewController alloc]init];
        self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _nv=[[UINavigationController alloc]initWithRootViewController:vc];
        self.window.backgroundColor=[UIColor whiteColor];
        self.window.rootViewController=_nv;
        [self.window makeKeyAndVisible];
        return YES;
    }

这两种情况分别代表着self.window.rootViewController为上面提到的两个独立的UINavigationController
从登陆注册界面,跳转到内容界面,此处只能用presentViewController 来推出一个UINavigationController ,用push来推出一个UINavigationController会爆炸,下面两个二选一,貌似都可以,目前还不明白有什么区别

[self presentViewController:newVc animated:YES completion:nil];
[self.navigationController presentViewController:newVc animated:YES completion:nil];

现在,以及通过登陆注册界面进入了内容界面,接下来就是在合适的地方要能退回到登陆注册界面进行换账号等操作

if (self.navigationController.presentingViewController) {
        NSLog(@"上一级存在");
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];

    }
    else{
        NSLog(@"上一级不存在");
        UIWindow *window = [[[UIApplication sharedApplication] delegate] window];  // 获得根窗口
        ViewController *vc=[[ViewController alloc]init];
        UINavigationController *nv=[[UINavigationController alloc]initWithRootViewController:vc];
        window.rootViewController=nv;
        CATransition *myTransition=[CATransition animation];//创建CATransition
        myTransition.duration=0.35;//持续时长0.35秒
        myTransition.timingFunction=UIViewAnimationCurveEaseInOut;//计时函数,从头到尾的流畅度
        myTransition.type = kCATransitionReveal;//子类型
        [window.layer addAnimation:myTransition forKey:nil];
    }

判断内容界面的navigationController管理器是被上一级present出来的。

  • 如果是,那就反过来dismiss掉就退回到登陆注册界面,内容界面也得到释放
  • 如果不是,那dismiss就没有用了,这时则直接更改window的根视图为需要跳转的navigationController,为了看上去更平滑,加上动画效果CATransition,动画效果选择不同的myTransition.type

到此告一段落,仅当抛砖引玉

相关文章

网友评论

      本文标题:两个UINavigationController 如何相互切换

      本文链接:https://www.haomeiwen.com/subject/pjkepttx.html