当前位置: 移动技术网 > 移动技术>移动开发>Android > 代码示例:添加按键使得可以从当前视图控制器跳到另一个

代码示例:添加按键使得可以从当前视图控制器跳到另一个

2020年07月17日  | 移动技术网移动技术  | 我要评论

示例:rootViewController和ViewController之间的互相跳转

import UIKit

class RootViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.green
        
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 50)
        button.backgroundColor = UIColor.white
        button.setTitle("打开新的视图控制器", for: .normal)
        button.setTitleColor(UIColor.gray, for: .normal)
        view.addSubview(button)
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
    }

    func buttonClick() {
        navigationController?.pushViewController(ViewController(), animated: true)
        // 另一种跳转方式
        // present(ViewController(), animated: true, completion: nil)
    }

}
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.red
       
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 50)
        button.backgroundColor = UIColor.white
        button.setTitle("回到RootViewController", for: .normal)
        button.setTitleColor(UIColor.gray, for: .normal)
        view.addSubview(button)
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
    }

    func buttonClick() {
        navigationController?.popViewController(animated: true)
        // 另一种跳转方式 成对出现
        dismiss(animated: true, completion: nil)
    }
}

本文地址:https://blog.csdn.net/weixin_44337445/article/details/107374368

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网