Programmatically switching between tabs within Swift

后端 未结 8 545
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 02:32

I need write some code to switch the view to another tab when the iOS app starts (so, for example, the second tab is shown by default rather than the first).

I\'m ne

8条回答
  •  庸人自扰
    2020-11-30 03:34

    1.Create a new class which supers UITabBarController. E.g:

    class xxx: UITabBarController {
    override func viewDidLoad() {
            super.viewDidLoad()
    }
    

    2.Add the following code to the function viewDidLoad():

    self.selectedIndex = 1; //set the tab index you want to show here, start from 0
    

    3.Go to storyboard, and set the Custom Class of your Tab Bar Controller to this new class. (MyVotes1 as the example in the pic)

    enter image description here

提交回复
热议问题