ingenspor
ingenspor

Reputation: 932

Make a delegate for my TabBar in AppDelegate using XCode 4 with storyboard

I'm making my first app. I use Xcode 4 with storyboard to get a better picture of whats going on. The app starts (in the storybard) with a TabBarController with 4 tabs. Two of the tabs are navigation controllers with tableview systems, and the other two tabs are regular viewcontrollers.

I've made the TabBarController itself in the Storyboard only, not made a class for it or anything. So all the tabs are "running" at the same time in the app it seems, they remain unchanged when I enter another tab and back. It looks kind of unprofessional, and maybe bad for iphone too?

Now I want to make the left tab become unactive/reset to the RootViewController when another tab is entered.

In other words make some kind of delegate function for the TabBar so only the selected tab is active. Something like that. Correct me if I'm wrong, I'm just trying to make an understanding of what I need to do.

After what I understand after my research, all I have to make a property for the TabBar in the AppDelegate.h, synthesize it in AppDelegate.m then somehow make the TabBar's delegate to AppDelegate (self ?) or something, that's done in here somehow (AppDelegate.m):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//some statements.

then add something like this somewhere in the AppDelegate.m:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
//With some statements about the viewcontrollers

But as you see I'm not very experienced with coding, and I can't find a tutorial/guide I understand because they use xibs etc.. So can someone please help me make this function? I will need code examples to understand how to do it, if you only describe the process in words it will just make lots of new questions for me if you understand what I mean.

The classes that reprecent the 4 tabs (the 2 tableviews and the 2 views) let's call them:

FirstTableViewController, SecondTableViewController, FirstViewController, SecondViewController

I dont know if it's enough to make this function in the AppDelegate, or if I have to make a class (.h/.m) for the TabBar, add codes in the different ViewControllers classes etc.

I hope someone can help me do this in the most easy and correct way, I'm sure many others bounce into this dilemma in their beginner stage of app development and will find the answer to this very useful.

Thank you for your help.

Upvotes: 0

Views: 2816

Answers (1)

Ben Scheirman
Ben Scheirman

Reputation: 40951

This is the behavior that Apple intended and users expect from a tab bar based application. It allows you to view different areas of an application without losing where you are.

However, if, under certain circumstances, you want to reset a view controller back to its original state, you can do this in viewWillAppear of a given view controller.

If you want to handle this via the app delegate & tab bar controller's delegate methods, you have the reference to the tab bar after your Storyboard / XIB has loaded. Just use:

rootViewController.tabBarController.delegate = self inside of your app delegate.

Alternatively, you can change the class of tabBarController to your own custom class in the storyboard by selecting the tab bar controller in the objects list on the left pane and going to the identity inspector, changing the class from UITabBarController to a custom subclass.

Upvotes: 2

Related Questions