views:

1612

answers:

4

Sorry for the newbie question. I have a UITabBar in my main window view as well as an array of UINavigationControllers for each Tab. The structure is similar to the iPod app in that the main views can be seen by selecting TabBar items and then the user can drill down further with the NavigationController by pushing views to the stack.

What I would like to be able to do is to do the equivalent of pressing a TabBar button at the bottom from any of the subviews in code (i.e., change the selected property of the TabBar and display launch the first view controller for the tab).

Any help would be greatly appreciated.

Dave

A: 
myTabBar.selectedItem = [myTabBar.items objectAtIndex: index];

To select the tabbar's item at custom index.

Vladimir
+5  A: 
[myTabBarController setSelectedIndex:index]

EDIT: Answering the part 2 question from the comment:

You can define a method in AppDelegate for switching to a different tab.

And you can get hold of appdelegate from anywhere and send a message.. something like:

 MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
 [appDelegate SwitchToTab:index]
Prakash
To me, setting by calling Tabbarcontroller method seems proper! But will check it doesn't matter!
Prakash
Yes, may be you are right - I took a part of my code where I used TabBar without tabbarcontroller
Vladimir
This is great thanks. OK, dumb question part 2. How do I get to the root object from my code? The actual TabBar is set up in code from the root View Controller. I want to be able to set the property of the TabBar but from another view controller that was pushed to the stack. Am I explaining that well enough. It is like I need an equivalent of self but for the parent object at the top of the tree.Again sorry for the newbe question, I'm missing something quite fundamental in my thinking I guess.
Magic Bullet Dave
do I need something like (pseudo code):[parentobject.TabBarController setSelectedIndex:index]
Magic Bullet Dave
Brilliant, thank you so much.
Magic Bullet Dave
A: 

alternatively...

[self.parentViewController.tabBarController setSelectedIndex:3];
codemonkey
A: 

I'd like to reply to Prakash, but can't figure out how. Maybe I'm blocked until my score goes up.

Anyhow, I hope this helps someone:

I was doing what Prakash said, and nothing was happening. It's because to get a pointer to my app delegate, I was doing this:

AppDelegate_Phone *appDelegate = [[AppDelegate_Phone alloc] init];

When I should have been doing this:

AppDelegate_Phone *appDelegate = (AppDelegate_Phone *) [[UIApplication sharedApplication] delegate];

Newbie mistake.

Rich Joslin