swift uinavigationController 视图控制器切换(二)
来源:互联网 发布:知乎和百度有什么区别 编辑:程序博客网 时间:2024/05/17 23:07
我们接着上节继续看返回指定ViewController
首先再创建两个UIViewController的子类
分别命名:ThirdViewController,ForthViewController
然后我们分别在SecondViewController 和 ThirdViewController上添加button点击后跳转下一个页面
SecondViewController代码
- import UIKit
- class SecondViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- self.title="第二个页面"
- let btn=UIButton(frame: CGRectMake(20, 120, 320, 36))
- btn.setTitle("弹出第三个视图", forState: UIControlState.Normal)
- btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
- btn.addTarget(self, action: "openAct", forControlEvents: UIControlEvents.TouchDown)
- self.view.addSubview(btn)
- }
- func openAct(){
- let thirdVC=ThirdViewController()
- self.navigationController?.pushViewController(thirdVC, animated: true)
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
- // Get the new view controller using segue.destinationViewController.
- // Pass the selected object to the new view controller.
- }
- */
- }
ThirdViewController代码
- import UIKit
- class ThirdViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- self.title="第三个页面"
- let btn=UIButton(frame: CGRectMake(20, 120, 320, 36))
- btn.setTitle("弹出第四个视图", forState: UIControlState.Normal)
- btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
- btn.addTarget(self, action: "openAct", forControlEvents: UIControlEvents.TouchDown)
- self.view.addSubview(btn)
- }
- func openAct(){
- let forthVC=ForthViewController()
- self.navigationController?.pushViewController(forthVC, animated: true)
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
- // Get the new view controller using segue.destinationViewController.
- // Pass the selected object to the new view controller.
- }
- */
- }
我们执行代码就发现已经可以跳转了
接下来我们给FortthViewController 添加三个button。
- let btn=UIButton(frame: CGRectMake(20, 120, 320, 36))
- btn.setTitle("回到第一个页面", forState: UIControlState.Normal)
- btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
- btn.tag=91
- btn.addTarget(self, action: "openAct:", forControlEvents: UIControlEvents.TouchDown)
- self.view.addSubview(btn)
- let btn2=UIButton(frame: CGRectMake(20, 170, 320, 36))
- btn2.setTitle("回到第二个页面", forState: UIControlState.Normal)
- btn2.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
- btn2.tag=92
- btn2.addTarget(self, action: "openAct:", forControlEvents: UIControlEvents.TouchDown)
- self.view.addSubview(btn2)
- let btn3=UIButton(frame: CGRectMake(20, 220, 320, 36))
- btn3.setTitle("回到第三个页面", forState: UIControlState.Normal)
- btn3.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
- btn3.tag=93
- btn3.addTarget(self, action: "openAct:", forControlEvents: UIControlEvents.TouchDown)
- self.view.addSubview(btn3)
- func openAct(btn:UIButton){
- if btn.tag==91{
- //回到首页
- self.navigationController?.popToRootViewControllerAnimated(true)
- }else if btn.tag==92{
- //通过堆栈顺序获取要跳转的视图控制器
- let vc=self.navigationController?.viewControllers[1] as! UIViewController
- self.navigationController?.popToViewController(vc, animated: true)
- }else if btn.tag==93{
- //返回到上一个页面
- self.navigationController?.popViewControllerAnimated(true)
- }
- }
我们点击按钮看下效果
这里要说明的一点是 跳转到指定ViewController中我们首先调用了NavigationController的viewControllers[1]来获取了要跳转到的视图控制器。视图控制器跳转的时候是按照栈的方式来的.
我们例子中的首页是第一次出现在navigationController中的,所以栈底就是我们需要的首页,我们可以使用viewControllers[0] 来测试一下看能不能跳转首页
0 0
- swift uinavigationController 视图控制器切换(二)
- UINavigationController视图控制器切换(二)
- UINavigationController视图控制器切换(一)
- UINavigationController视图控制器切换(一)
- Iphone开发(十三)用导航控制器实现视图的分层切换(UINavigationController)
- Iphone开发(十三)用导航控制器实现视图的分层切换(UINavigationController)
- Iphone开发(十三)用导航控制器实现视图的分层切换(UINavigationController)
- swift UINavigationController 导航控制器
- 导航视图控制器UINavigationController
- IOS: UINavigationController 视图控制器
- UINavigationController 导航视图控制器
- UINavigationController多视图控制器
- UINavigationController多视图控制器
- UINavigationController视图控制器
- UINavigationController的视图切换
- Iphone开发用导航控制器实现视图的分层切换(UINavigationController)
- 获取UIView的视图控制器(Swift)
- 视图控制器切换动画
- 字符串匹配
- Java双向链表实现
- Zend Framework2 入门教程
- 二分幂、快速幂、矩阵快速幂、幂取模
- IOS在子线程中使用定时器,将定时器添加至RunLoop中
- swift uinavigationController 视图控制器切换(二)
- 手动刷微信运动步数
- swift UINavigationController属性设置
- 内联标签和块标签
- Fragment第二次执行数据为空
- Android开发之创建Fragment
- 以人为本 | 如何保证高质量的软件交付
- swift 设置 navigation左右两侧按钮
- [LeetCode15]3Sum