-2

I am new to swift programming and I have a Tabbed application with 2 UITabBar items , I want that when user taping the second tab, application set the selected tab index to 0 and move user to the first tab. I tried this code in my SecondViewController.swift's viewDidLoad :

self.tabBarController?.selectedIndex = 0

but it doesn't work.

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
Mahdi Appleseed
  • 79
  • 1
  • 3
  • 11

1 Answers1

3

This works:

override func viewDidAppear() {
    self.tabBarController?.selectedIndex = 0
}

See this answer for a good explanation of viewDidLoad() vs. viewDidAppear.

Relevant excerpt:

This is where you want to perform any layout actions or do any drawing in the UI - for example, presenting a modal view controller

Community
  • 1
  • 1
allocate
  • 1,323
  • 3
  • 14
  • 28