简体   繁体   中英

How to use a UITabBar without UITabBarController?

I want to implement a tab bar in the top of the iPhone screen, and want to be able to navigate in the different views. I have used the delegate method, here is how it looks in the storyboard:

点击此处查看屏幕截图

However, as I cannot delegate the TabBar to multiple containers, it seems the code that I have implemented doesn't work, here it is:

class PersonnesController: UIViewController, UITabBarDelegate {

    @IBOutlet weak var tabBar : UITabBar!
    @IBOutlet weak var comptesContainer : UIView!
    @IBOutlet weak var membreContainer : UIView!
    @IBOutlet weak var discussionContainer : UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        tabBar.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tabBarDelegate(tabBar: UITabBar!, didSelectItem item: UITabBarItem!) {

        switch item.tag {
            case 1:
                comptesContainer.isHidden = false
                membreContainer.isHidden = true
                discussionContainer.isHidden = true

            case 2:
                comptesContainer.isHidden = true
                membreContainer.isHidden = false
                discussionContainer.isHidden = true

            case 3:
                comptesContainer.isHidden = true
                membreContainer.isHidden = false
                discussionContainer.isHidden = true

            default:
                comptesContainer.isHidden = false
                membreContainer.isHidden = true
                discussionContainer.isHidden = true
        }
    }
}

The real problem seems to be that I can only delegate the TabBar to only one container without being to delegate to the three. How could I achieve that?

PS : The best response I have found yet is here but I fail to implement it, I don't know how he/she manages to delegate multiple times.

My question would be, why not using the UITabBarController? Just implementing the delegate will not do what you try to do, as long as noone is calling into the delegate (which is what the UITabBarController does/is for).

Are you sure the tabbar's delegate is set to the view contoller?

override func viewDidLoad() {
    super.viewDidLoad()
    tabBar.delegate = self
}

Can you try this and see if the delegate fuction is being called?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM