0

I am making a swift app that includes a tutorial. When I attempt to segue back to the main controller, the tab bar items turns grey (They should be white). This is because I set-up the tab bar in the app delegate. Is there a way to programmatically call the app delegate from a view-controller?

I am wanting to call the didFinishLaunchingWithOptions() function

Hoovin Shmoovin
  • 161
  • 2
  • 13
  • 1
    Possible duplicate of [How do I get a reference to the app delegate in Swift?](https://stackoverflow.com/questions/24046164/how-do-i-get-a-reference-to-the-app-delegate-in-swift) – Pochi Jul 13 '17 at 01:30
  • I would try to avoid putting a ton of custom code in your AppDelegate and the referencing it throughout your app. My recommendation would be to subclass UITabBarController and add whatever logic you need inside of that. You should be able to get a reference to your UITabBarController from other UIViewController subclasses. – adrum Jul 13 '17 at 01:34

2 Answers2

1
    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
        //do something with appDelegate
    }
Yun CHEN
  • 6,450
  • 3
  • 30
  • 33
1

If you mean that you have a method in your app delegate that sets the tab bar to a custom color. Then yes, (Replace AppDelegate to your app delegate's class name if you changed it):

let appDelegate = UIApplication.shared.delegate as? AppDelegate

appDelegate?.yourCustomizeMethod()

If you mean, you are customizing your tab bar inside the "didFinishLaunchingWithOptions" method, then you will have to extract it into its own custom function.

Pochi
  • 13,391
  • 3
  • 64
  • 104
  • something like what? – Pochi Jul 13 '17 at 01:57
  • ` if let appDelegate = UIApplication.shared.delegate as? AppDelegate { didFinishLaunchingWithOptions() }` – Hoovin Shmoovin Jul 13 '17 at 01:57
  • No, that method has arguments which you are not passing. Just make another function in your app delegate with just the code that customizes your interface. Then call that method within the didFinishLaunchingWithOptions. Additionally, call that method by itself as explained in my answer. Then again, this is not really recommended. Your app delegate should not be bloated with functions. Ideally you would want to subclass your viewcontroller so it applies the customising code. Or you could create an utility class that does the same thing. – Pochi Jul 13 '17 at 02:00