从子视图控制器推送视图控制器

时间:2019-04-09 13:37:11

标签: ios swift uiviewcontroller uinavigationcontroller childviewcontroller

我有一个应用程序,其中有一个“ DadViewController”,其中包含一个启用分页的UIScrollView。每个页面都填充有不同的UIViewController

如何在UIViewController所包含的一个控制器内,通过按钮轻按新的UIScrollView

class DadView: UIView {

    let model = DadModel()

    let scrollView: UIScrollView = {
        let view = UIScrollView()
        view.isPagingEnabled = true
        // Additional setup...
        return view
    }()



    override init(frame: CGRect) {
        super.init(frame: frame)
        setupScrollView()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



    fileprivate func setupScrollView() {
        self.addSubview(scrollView)
        // Autolayout code to pin scrollview to all 4 sides

        let vc1 = VC1()
        scrollView.addSubview(vc1.view)
        vc1.view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)

        // Add additional view controller views with the appropriate frames...

    }
}

class DadController: UIViewController {
    var dadView: DadView!

    override func loadView() {
        super.loadView()
        dadView = DadView()
        view = dadView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

class VC1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green
    }

}

1 个答案:

答案 0 :(得分:0)

更改您的DadView setupScrollView以接受UIViewController实例作为参数。而且不要在init中调用此方法

class DadView: UIView {

    let model = DadModel()

    let scrollView: UIScrollView = {
        let view = UIScrollView()
        view.isPagingEnabled = true
        // Additional setup...
        return view
    }()



    override init(frame: CGRect) {
        super.init(frame: frame)
//        setupScrollView()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



    fileprivate func setupScrollView(_ parentVC: UIViewController) {
        self.addSubview(scrollView)
        // Autolayout code to pin scrollview to all 4 sides

        let vc1 = VC1()
        vc1.view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        parentVC.addChild(vc1)
        scrollView.addSubview(vc1.view)
        vc1.didMove(toParent: parentVC)

        // Add additional view controller views with the appropriate frames...

    }
}

在创建DadView实例后,在DadViewController中使用self调用setupScrollView方法

class DadController: UIViewController {
    var dadView: DadView!

    override func loadView() {
        super.loadView()
        dadView = DadView()
        dadView.setupScrollView(self)
        view = dadView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

然后您可以从子viewcontroller获取父视图控制器并执行push

class VC1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green
    }
    @IBAction func buttonAction(_ sender: UIButton) {
        self.parent?.navigationController?.pushViewController(NewVC(), animated: true)
    }
}