ios swift学习笔记简友广场

swift学习笔记--页面跳转

2020-05-16  本文已影响0人  迷夏湖

今天学习了下swift里通过present/dismiss方式实现页面的跳转。下面通过一个例子:页面A->页面B->C, 然后依次返回之前的页面,来演示下这一功能。

除了工程自动生成的GameViewController外,再新建两个ViewController1, ViewController2,
第一个页面放开始按钮, 第二个页面放 返回、下一个页面按钮,第三个页面放返回按钮。然后在需要跳转到下一个页面的按钮事件中调用present方法展示下一个页面, 在需要返回的按钮事件中调用dismiss方法返回。

class GameViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.lightGray
        addBtn()
    }
    
    func addBtn() {
        let wid = view.frame.width
        let hei = view.frame.height
        let startBtn = UIButton(frame: CGRect(x: wid/2 - 40, y: hei/3, width: 80, height: 40))
        let settingBtn = UIButton(frame: CGRect(x: wid/2 - 40, y: hei/3 + 80, width: 80, height: 40+40))
        setBtn(title: "开 始", btn: startBtn)
        setBtn(title: "设 置", btn: settingBtn)
        // 跳转到新的页面
        startBtn.addTarget(self, action: #selector(goToMainView), for: .touchUpInside)
    }
    
    func setBtn(title: String, btn: UIButton) {
        btn.setTitle(title, for: .normal)
        btn.contentHorizontalAlignment = .center
      //  btn.contentVerticalAlignment = .center
        btn.setTitleColor(UIColor.blue, for: .normal)
        view.addSubview(btn)
    }
    
    @objc func goToMainView() {
        //self.present(MainViewController(), animated: true, completion: nil)
        let nextVc = ViewController1()
        nextVc.setParentVc(parentVc: self)
        self.present(nextVc, animated: false, completion: nil)
    }
class ViewController1: UIViewController {
    
    var parentVc : UIViewController!
    
    func setParentVc(parentVc : UIViewController) {
        self.parentVc = parentVc
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.lightGray
        addBtn()
    }    
    func addBtn() {
        let wid = view.frame.width
        let hei = view.frame.height
        let backBtn = UIButton(frame: CGRect(x: wid/2 - 60, y: hei/3, width: 120, height: 40))
        let nextBtn = UIButton(frame: CGRect(x: wid/2 - 60, y: hei/3 + 80, width: 120, height: 40+40))
        setBtn(title: "返 回", btn: backBtn)
        setBtn(title: "下一个页面", btn: nextBtn)
        // 跳转到新的页面
        backBtn.addTarget(self, action: #selector(goBackView), for: .touchUpInside)
        nextBtn.addTarget(self, action: #selector(goNextView), for: .touchUpInside)
    }   
    func setBtn(title: String, btn: UIButton) {
        btn.setTitle(title, for: .normal)
        btn.contentHorizontalAlignment = .center
        //  btn.contentVerticalAlignment = .center
        btn.setTitleColor(UIColor.blue, for: .normal)
        view.addSubview(btn)
    }    
    @objc func goBackView() {
        parentVc.dismiss(animated: false, completion: nil)
    }
    @objc func goNextView() {
        let nextVc = ViewController2()
        nextVc.setParentVc(parentVc: self)
        self.present(nextVc, animated: false, completion: nil)
    }
}
class ViewController2: UIViewController {  
    private var parentVc : ViewController1!    
    func setParentVc(parentVc : ViewController1) {
        self.parentVc = parentVc
    }    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.lightGray
        addBtn()
    }    
    func addBtn() {
        let wid = view.frame.width
        let hei = view.frame.height
        let backBtn = UIButton(frame: CGRect(x: wid/2 - 40, y: hei/3, width: 80, height: 40))
        setBtn(title: "返 回2", btn: backBtn)
        // 跳转到新的页面
        backBtn.addTarget(self, action: #selector(goBackView), for: .touchUpInside)
    }    
    func setBtn(title: String, btn: UIButton) {
        btn.setTitle(title, for: .normal)
        btn.contentHorizontalAlignment = .center
        //  btn.contentVerticalAlignment = .center
        btn.setTitleColor(UIColor.blue, for: .normal)
        view.addSubview(btn)
    }
    @objc func goBackView() {
        parentVc.dismiss(animated: false, completion: nil)
    }
}

需要注意的是, dismiss方法的调用者应该是调用present的ViewController,所以在ViewController1和ViewController2中加入了父Controller的引用, 这样可以实现从页面3直接跳回页面1。 如果只是当层跳转返回,不需要保存父页面的引用,直接用self即可。

上一篇 下一篇

猜你喜欢

热点阅读