Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tabBar viewControllers in IB: send custom init?

My tabBarController-based app has several tabs. Each has a custom viewController class, the only difference being the way the instance is initialized. Is there a way to make interface builder send the different tabs custom init parameters?

Currently I'm doing the initialisation in viewWillAppear, but for a bunch of reasons it would make sense to do it in IB instead of in the code.

Any suggestions?

thanks, Kelso

like image 431
kbanman Avatar asked Jan 31 '26 08:01

kbanman


1 Answers

Interface Builder creates an archive of objects that is unarchived when you program executes. You can't really tell IB to call particular methods.

If you need to initialize before viewWillAppear: is called, you can do so in awakeFromNib, which is guaranteed to be called after all objects have been loaded and all outlets have been connected to their targets.

If you want to do initialization even earlier, you can do so by overriding initWithCoder: (see the NSCoding protocol for documentation). I don't know if it is documented anywhere, but that is the designated initialized for objects being decoded from an archive.

In all of the above, you won't be able to receive parameters, but in the code you should be able to access whatever you need with some judicious use of global variables. You can also use [[UIApplication sharedApplication] delegate] to get access to your application delegate object.

like image 123
benzado Avatar answered Feb 03 '26 07:02

benzado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!