1

I have below code for creating UITabBar , but don't know how to add action for tabs for switching between them.

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let item1 = UITabBarItem()
        let item2 = UITabBarItem()
        let bar = UITabBarController()
        let view1 = UIViewController()
        let view2 = UIViewController()

        bar.viewControllers = [view1 , view2]
        let tab = bar.tabBar as UITabBar
        item1.title = "Hi"
        item2.title = "bye"
        view.addSubview(tab)
        view1.tabBarItem = item1
        view2.tabBarItem = item2



    }
}
Hamish
  • 78,605
  • 19
  • 187
  • 280
AnaHejazi
  • 49
  • 1
  • 9
  • If you mean to set the selected view controller programmatically: http://stackoverflow.com/questions/2325780/how-to-set-the-tab-bar-item-1-to-be-selected-by-default-in-iphone – Surely Jan 17 '16 at 07:42

1 Answers1

1

Since your intent seems to be switching view controllers, you should use a UITabBarController. And then, you switch ViewControllers by assigning either selectedViewController or selectedIndex. Read more: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/#//apple_ref/occ/instp/UITabBarController/selectedViewController

Anticro
  • 685
  • 4
  • 12