IOS 视图跳转的总结

1、[self.view addSubView:view];和[self.window addSubView:view];需要注意,此方法只是把页面(view)加在当前页面(view)上,控制器(viewController)还是原来那个控制器。此时再用[self.navigationColler pushViewController:animated:];和 [self.navigationController popViewControllerAnimated:]; 是不行的。要想使用pushViewController和popViewController进行视图间的切换,就必须要求当前视图是个NavigationController。

2、有NavigationController导航栏的话,使用[self.navigationColler pushViewController:animated:];和[self.navigationController popViewControllerAnimated:];来进行视图切换。pushViewController是进入到下一个视图,popViewController是返回到上一视图。

3、没有NavigationController导航栏的话,使用[self presentViewController:animated:completion:];和[self dismissViewControllerAnimated:completion:];具体是使用可以从文档中详细了解。

4、要想使用pushViewController和popViewController来进行视图切换,首先要确保根视图是NavigationController,不然是不可以用的。这里提供一个简单的方法让该视图或者根视图是NavigationController。自己定义个子类继承UINavigationController,然后将要展现的视图包装到这个子类中,这样就可以使NavigationController了。提供的这个方法有很好的好处,就是可以统一的控制各个视图的屏幕旋转。将一个控制器(UIViewController)包装成一个导航控制器(UINavigationController):

UIViewController *vc = [[UIViewController alloc] init];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

1. 用UINavigationController的时候用

----进入下一个视图[self.navigationColler pushViewController:animated:];

----返回之前的视图[self.navigationController popViewControllerAnimated:];

----ps:push以后会在navigation的left bar自动添加back按钮,它的响应方法就是返回。所以一般不需要写返回方法,点back按钮即可。

2. 其他时候(视图不是UINavigationController的时候,只是一个viewController时)

----进入下一个视图:[self presentViewController:animated:completion:];

----返回之前的视图:[self dismissViewControllerAnimated:completion:];

3. 切换视图一般用不到addSubview

UINavigationController是导航控制器,如果pushViewController的话,会跳转到下一个ViewController,点返回会回到现在这个ViewController;如果是addSubview的话,其实还是对当前的ViewController操作,只是在当前视图上面又“盖”住了一层视图,其实原来的画面在下面呢,看不到而已。(当然,也可以用insertSubView  atIndex那个方法设置放置的层次)。

4.另加一个:

使用presentViewControllerAnimated方法从A->B->C,若想在C中直接返回A,则可这样实现:

C中返回事件:

  • void back
  • {
  • [self dismissViewControllerAnimated:NO];//注意一定是NO!!
  • [[NSNotificationCenter  defaultCenter]postNotificationName:@"backback" object:nil];
  • }

    然后在B中,

    1. //在viewdidload中:
    2. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(back) name:@"backback" object:nil];
    3. -(void)back
    4. {
    5. [self dismissViewControllerAnimated:YES];
    6. }

转载 http://www.cnblogs.com/zhongfeng/p/4413938.html

时间: 2024-08-12 14:19:41

IOS 视图跳转的总结的相关文章

iOS开发 跳转场景的三种方式

假设A跳转到B,三种方法: 1.按住ctrl键,拖动A上的控件(比如说UIButton)到B上,弹出菜单,选择Modal.不需要写任何代码,在A上点击Button就会跳转到B 2. 按住ctrl键,拖动A上的View Controller到B上,弹出菜单,选择Modal,两个场景间自动添加连接线和图标,选中该图标,打开Storyboard Segue,identifier输入一个标识符,这里以”aaaa”为例.A里需要跳转时,执行下面的代码: 1 [self performSegueWithId

iOS页面跳转及数据传递

iOS页面跳转: 第一种 [self.navigationController pushViewController:subTableViewController  animated:YES]; //描述:通过 NSNavigationBar 进行跳转 [self.navigationController popViewControllerAnimated:YES]; //描述:在子视图返回到上级视图 第二种 UIViewController *control = [[UIViewControl

UI03-viewController 视图跳转:

/*  ******今日任务:登陆界面 进行最后的优化 ,封装控件 (2)viewController (3)MVC 思想(4)对于屏幕旋转,内存异常的处理,(5)容器视图控制器,四个方法的使用 视图切换的方式 1.登陆界面的优化, (1)封装的设计模式的,组合类模式的装饰者模式,复合设计模式,23种设计模式    一个自定义控件里面有多个系统控件, (1)自定义一个视图,基于UI view,把包含的系统控件作为.h里的属性, (2)在.m中重写初始化方法,把各个系统控件的属性写死,注意各个控件

IOS 视图传值

一 视图跳转 <StoryBoard下的视图跳转> 我们知道:segue共有三种类型:push,modal,和custom.如下图://01 简单说下这三个的作用:使用导航栏压进新的控制器(push),模态的加载视图控制器(modal),自定义(custom). 好了,废话少说,现在开始我们的旅行. 1,首先建立一个Single View 模板的项目,记得勾选上storyboard.然后打开它,在rootViewController(也就是我们的主视图)添加一些label和一个button.

Swfit中视图跳转

1.跳转到任一UIViewController var sb = UIStoryboard(name: "Main", bundle:nil) var vc = sb.instantiateViewControllerWithIdentifier("ChooseViewController") as ChooseViewController self.presentViewController(vc, animated:true, completion:nil) 2

ios &ndash; 视图控制器如何管理视图?

移动设备的屏幕有限,所有的东西都需要放到一个单一窗口组成的单一界面显示,在ios中体现为视图切换(在<ios – 视图>中已经说明了视图),当一个视图替换掉另一个视图的时候,会经常使用动画效果,这个任务就是交给视图管理器来完成的. ios5之后应用程序窗口有一个根视图控制器(rootViewController),当不为rootViewController赋值时,会出现"Application windows are expected to have a root view cont

dismissViewController实现多个模态视图跳转

最近实现的一个需求需要用到模态视图,所以少不了与dismissViewController方法打交道.本文主要讲一讲在使用dismissViewController方法过程中遇到的那些坑. 由于业务逻辑比较特殊,程序中需要在A试图控制器中present B试图控制器的视图,B视图控制器需要present C视图控制器中的视图.最后从C直接返回A. dismissViewControllerAnimated方法并不难用,其原型为: - (void)dismissViewControllerAnim

[100天Swift]第五天:用代码实现视图跳转

前言: 在Xcode推出StoryBoard的时候,Xcode可以更简单的,更聪明的帮助我们做一些简单的事情,快速建立一个Base的App,可是作为一个习惯了写代码的“码农”,还是需要知道如何通过代码去实现这些“简单”的事情. Swift中的视图跳转 1.跳转到任一UIViewController var sb = UIStoryboard(name: "Main", bundle:nil) var vc = sb.instantiateViewControllerWithIdenti

iOS 10 跳转系统设置

苦心人天不负, 为了项目终于把 iOS 10 跳转系统设置的方法给搞定了, 很欣慰. http://www.cnblogs.com/lurenq/p/6189580.html iOS 10 跳转系统设置的字段 电池电量 Prefs:root=BATTERY_USAGE 通用设置 Prefs:root=General 存储空间 Prefs:root=General&path=STORAGE_ICLOUD_USAGE/DEVICE_STORAGE 蜂窝数据 Prefs:root=MOBILE_DAT