swift uinavigationController 视图控制器切换(二)

来源:互联网 发布:知乎和百度有什么区别 编辑:程序博客网 时间:2024/05/17 23:07

我们接着上节继续看返回指定ViewController

首先再创建两个UIViewController的子类

分别命名:ThirdViewController,ForthViewController


然后我们分别在SecondViewController 和 ThirdViewController上添加button点击后跳转下一个页面

SecondViewController代码

[objc] view plaincopy
  1. import UIKit  
  2.   
  3. class SecondViewController: UIViewController {  
  4.   
  5.     override func viewDidLoad() {  
  6.         super.viewDidLoad()  
  7.   
  8.         // Do any additional setup after loading the view.  
  9.         self.title="第二个页面"  
  10.           
  11.         let btn=UIButton(frame: CGRectMake(2012032036))  
  12.         btn.setTitle("弹出第三个视图", forState: UIControlState.Normal)  
  13.         btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)  
  14.         btn.addTarget(self, action"openAct", forControlEvents: UIControlEvents.TouchDown)  
  15.         self.view.addSubview(btn)  
  16.           
  17.     }  
  18.     func openAct(){  
  19.         let thirdVC=ThirdViewController()  
  20.         self.navigationController?.pushViewController(thirdVC, animatedtrue)  
  21.     }  
  22.   
  23.     override func didReceiveMemoryWarning() {  
  24.         super.didReceiveMemoryWarning()  
  25.         // Dispose of any resources that can be recreated.  
  26.     }  
  27.       
  28.   
  29.     /* 
  30.     // MARK: - Navigation 
  31.  
  32.     // In a storyboard-based application, you will often want to do a little preparation before navigation 
  33.     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
  34.         // Get the new view controller using segue.destinationViewController. 
  35.         // Pass the selected object to the new view controller. 
  36.     } 
  37.     */  
  38.   
  39. }  


ThirdViewController代码


[objc] view plaincopy
  1. import UIKit  
  2.   
  3. class ThirdViewController: UIViewController {  
  4.   
  5.     override func viewDidLoad() {  
  6.         super.viewDidLoad()  
  7.   
  8.         // Do any additional setup after loading the view.  
  9.         self.title="第三个页面"  
  10.           
  11.         let btn=UIButton(frame: CGRectMake(2012032036))  
  12.         btn.setTitle("弹出第四个视图", forState: UIControlState.Normal)  
  13.         btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)  
  14.         btn.addTarget(self, action"openAct", forControlEvents: UIControlEvents.TouchDown)  
  15.         self.view.addSubview(btn)  
  16.     }  
  17.     func openAct(){  
  18.         let forthVC=ForthViewController()  
  19.         self.navigationController?.pushViewController(forthVC, animatedtrue)  
  20.     }  
  21.   
  22.     override func didReceiveMemoryWarning() {  
  23.         super.didReceiveMemoryWarning()  
  24.         // Dispose of any resources that can be recreated.  
  25.     }  
  26.       
  27.   
  28.     /* 
  29.     // MARK: - Navigation 
  30.  
  31.     // In a storyboard-based application, you will often want to do a little preparation before navigation 
  32.     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
  33.         // Get the new view controller using segue.destinationViewController. 
  34.         // Pass the selected object to the new view controller. 
  35.     } 
  36.     */  
  37.   
  38. }  

我们执行代码就发现已经可以跳转了

接下来我们给FortthViewController 添加三个button。

[objc] view plaincopy
  1. let btn=UIButton(frame: CGRectMake(2012032036))  
  2. btn.setTitle("回到第一个页面", forState: UIControlState.Normal)  
  3. btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)  
  4. btn.tag=91  
  5. btn.addTarget(self, action"openAct:", forControlEvents: UIControlEvents.TouchDown)  
  6. self.view.addSubview(btn)  
  7.   
  8. let btn2=UIButton(frame: CGRectMake(2017032036))  
  9. btn2.setTitle("回到第二个页面", forState: UIControlState.Normal)  
  10. btn2.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)  
  11. btn2.tag=92  
  12. btn2.addTarget(self, action"openAct:", forControlEvents: UIControlEvents.TouchDown)  
  13. self.view.addSubview(btn2)  
  14.   
  15. let btn3=UIButton(frame: CGRectMake(2022032036))  
  16. btn3.setTitle("回到第三个页面", forState: UIControlState.Normal)  
  17. btn3.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)  
  18. btn3.tag=93  
  19. btn3.addTarget(self, action"openAct:", forControlEvents: UIControlEvents.TouchDown)  
  20. self.view.addSubview(btn3)  

[objc] view plaincopy
  1. func openAct(btn:UIButton){  
  2.     if btn.tag==91{  
  3.         //回到首页  
  4.         self.navigationController?.popToRootViewControllerAnimated(true)  
  5.     }else if btn.tag==92{  
  6.         //通过堆栈顺序获取要跳转的视图控制器  
  7.         let vc=self.navigationController?.viewControllers[1] as! UIViewController  
  8.         self.navigationController?.popToViewController(vc, animatedtrue)  
  9.     }else if btn.tag==93{  
  10.         //返回到上一个页面  
  11.         self.navigationController?.popViewControllerAnimated(true)  
  12.     }  
  13. }  

我们点击按钮看下效果

这里要说明的一点是 跳转到指定ViewController中我们首先调用了NavigationController的viewControllers[1]来获取了要跳转到的视图控制器。视图控制器跳转的时候是按照栈的方式来的.

我们例子中的首页是第一次出现在navigationController中的,所以栈底就是我们需要的首页,我们可以使用viewControllers[0] 来测试一下看能不能跳转首页

0 0