Minitour / AZTabBarController

A custom tab bar controller for iOS written in Swift 4.2
MIT License
348 stars 65 forks source link

How do I get selectedViewController from AZTabBarController? #28

Closed akhilcb closed 6 years ago

akhilcb commented 6 years ago

Thanks for the great library. I am using this library through cocoapods and I have a query. I am trying to get selectedViewController from AZTabBarController similar to the same property in UITabBarController, but I am not able to locate it. Please let me know how I can get that. If possible please add the same to the library itself so that it would be useful to more people.

Minitour commented 6 years ago

@akhilcb Hi, at the moment there is no such property, I recommended that you keep an array that holds your controllers. However if you want a quick solution you can use this code:

Include this extension in the AZTabBarController.swift:

public extension AZTabBarController {
    public var currentTab: UIViewController? {
        if selectedIndex >= 0,selectedIndex < tabCount, let controller = controllers[selectedIndex] { return controller }
        return nil
    }
}

You can then call it like so:

let controller: UIViewController? = tabBar.currentTab
akhilcb commented 6 years ago

Thanks. This what I ended up doing. It would be really helpful for others too if you could add this as a property of AZTabBarController itself in next release. Especially when someone is switching from UITabBarController to AZTabBarController, this will help.

Minitour commented 6 years ago

@akhilcb Alright, I will include this in the next release. Thank you for your feedback.