Minitour / AZTabBarController

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

Setting "badgeText" from other view controllers #8

Closed daerimin closed 7 years ago

daerimin commented 7 years ago

Hi, awesome library - love it!! I'm a bit new to Swift and I need a bit of help. I need to be able to set the badge text from the other view controllers. Each view controller is responsible for updating separate pieces of info from the server, so they need to be able to set their badges. I tried making the tabController public, then saying self.parent.tabController from the child view controller - which WOULD work, except that I have no idea how to declare the tabController correctly as a public. It seems to require icons, etc, which I can't give outside viewDidLoad. Can you offer any guidance on this?

Thanks!

Minitour commented 7 years ago

@daerimin Hi, I have already received a similar question. Check out this issue here.

Let me know if you were able to resolve your issue.

daerimin commented 7 years ago

My apologies - I didn't see this other question. :)

I actually JUST found a way to handle this, and was on my way back here to explain and close this out.. I'm not sure if it's really a hack, or would be considered "legit"..

in MainViewController:

public var tabController = AZTabBarController.init(withTabIcons: [UIImage(named: "some_image")!])

then...

override func viewDidLoad() {
     var icons = [ ... actual array of UIImage ... ]
     self.tabController = AZTabBarController.insert(info: self, withTabIcons: icons)
     ... set all view controllers ...
}

then, from the other controllers I can do this:

(self.parent as! MainViewController).tabController.set(badgeText: badgeText, atIndex: index)

Like I said, this is probably breaking a bunch of rules I don't know about... but it's working. Feel free to tell me I'm crazy. I would believe it. :)

Minitour commented 7 years ago

I still recommend delegation, however you can still access the tabbar using the .parentproperty since it's the actual parent of the view controllers and not your MainViewController.

daerimin commented 7 years ago

Understood. I will make the effort to learn the correct way. Thank you very much for your time, and for the library!

Minitour commented 7 years ago

@daerimin I made a quick extension that I will be including in the next release of this pod. This will allow you to call the current instance of the tabBar directly from any view controller.

public extension UIViewController{

    public var currentTabBar: AZTabBarController?{
        var current: UIViewController? = parent

        while current != nil {
            if current is AZTabBarController{
                return current as? AZTabBarController
            }else{
                current = current?.parent
            }
        }

        return nil
    }
}
daerimin commented 7 years ago

You rock! This is WAY less ghetto than what I came up with. :) Thank you!

On 4/11/17 12:56 PM, Antonio Zaitoun wrote:

public extension UIViewController{

 public  var  currentTabBar:  AZTabBarController?{
     var  current:  UIViewController?  =  parent

     while  current!=  nil  {
         if  currentis  AZTabBarController{
             return  currentas?  AZTabBarController
         }else{
             current=  current?.parent
         }
     }

     return  nil
 }

}

Minitour commented 7 years ago

Happy to help 😉