UINavigationController视图控制器切换(二)

前端之家收集整理的这篇文章主要介绍了UINavigationController视图控制器切换(二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

上节地址:http://blog.csdn.net/lwjok2007/article/details/48346719

我们接着上节继续看返回指定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,36))
        btn.setTitle("弹出第四个视图",forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(btn)
    }
    func openAct(){
        let forthVC=ForthViewController()
        self.navigationController?.pushViewController(forthVC,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,36))
        btn.setTitle("回到第一个页面",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,36))
        btn2.setTitle("回到第二个页面",forState: UIControlState.Normal)
        btn2.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
        btn2.tag=92
        btn2.addTarget(self,forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(btn2)
        
        let btn3=UIButton(frame: CGRectMake(20,220,36))
        btn3.setTitle("回到第三个页面",forState: UIControlState.Normal)
        btn3.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
        btn3.tag=93
        btn3.addTarget(self,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] 来测试一下看能不能跳转首页


好了 基本简单的跳转都实现了。大家有问题欢迎加群讨论

苹果开发群 :414319235 欢迎加入 欢迎讨论问题

原文链接:https://www.f2er.com/swift/325983.html

猜你在找的Swift相关文章