Navigation controller simply with Swift?

时光总嘲笑我的痴心妄想 提交于 2019-12-16 18:03:31

问题


I have 3 view controllers I load from story board :

controller1  = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
controller2 = storyboard.instantiateViewController(withIdentifier: "ModelController") as! ModelController
controller3  = storyboard.instantiateViewController(withIdentifier: "BuyController") as! BuyViewController

I would like to create, and load these 3 controllers into a bottom navigation bar with 3 buttons.

Programmatically .

How would I do that with just a simple few lines of code ?


回答1:


I made a sample Project for you to do that

Check at - https://drive.google.com/open?id=1M12Nv4PLNUePSJFHf7w8b08M2_mWcUEC

Some refereeing code

My Three VC Objects

private lazy var ThirdObject: VC3 =
    {
        // Instantiate View Controller
        let viewController = self.storyboard?.instantiateViewController(withIdentifier: "VC3") as! VC3

        // Add View Controller as Child View Controller
        self.addChildViewController(viewController)
        return viewController
    }()


    private lazy var FirstObject: VC1 =
    {
        // Instantiate View Controller
        let viewController = self.storyboard?.instantiateViewController(withIdentifier: "VC1") as! VC1

        // Add View Controller as Child View Controller
        self.addChildViewController(viewController)
        return viewController
    }()


    private lazy var SecondObject: VC2 =
    {
        // Instantiate View Controller
        let viewController = self.storyboard?.instantiateViewController(withIdentifier: "VC2") as! VC2

        // Add View Controller as Child View Controller
        self.addChildViewController(viewController)
        return viewController
    }()

Storyboard View

Simulator screen shots

ScreenShot 1 - When no object added

ScreenShot 2 - When Clicked on any button

I clicked on second So showed as

Note: I had used Toolbar with three button You can do same by adding buttons in Navigation bar , Showed you Method how to do , Depend on you that want to make use of Navigation controller or ToolBar at bottom of screen



来源:https://stackoverflow.com/questions/48273772/navigation-controller-simply-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!