iOS使用presentViewController弹出页面和隐藏页面
在本例中使用的是 presentViewController
,在当前页面上层加载一个新页面(模态),这种方式一般出现在需要使用者完成某件事情,如输入密码、增加资料等操作后,才能(回到跳转前的控制器)继续。例如系统的 WIFI 连接输入密码提示。页面默认是从下至上弹出的(动画效果可改变),使用 dismissViewControllerAnimated
可以隐藏页面。
presentViewController
和 dismissViewControllerAnimated
的介绍如下:
弹出页面
针对 storyboard 制作的页面和手写的页面,需要使用两种不同方法,否则在模拟器上可能会显示黑屏。本例中使用Swift语言。
对于手写的页面,使用下面的方法:
@IBAction func showOne() { let vv = ViewController2() [self .presentViewController(vv, animated: true, completion: { () -> Void in // code })] }
使用 storyboard 制作的页面,可使用下面的方法:
@IBAction func showOne() { let sb = UIStoryboard(name: "Main", bundle: nil) let vc = sb.instantiateViewControllerWithIdentifier("myView2") //as ViewController2 self.presentViewController(vc, animated: true, completion: nil) }
- 其中 myView2 是在 Main.storyboard 中选中的
ViewController
的 storyboardID 值,可以在 Identifier inspector 中修改。
- 其中 myView2 是在 Main.storyboard 中选中的
隐藏页面
对于隐藏页面会简单一点:
@IBAction func hideOne() {
[self .dismissViewControllerAnimated(true, completion: { () -> Void in
// 代码
})]
}