Objective-c – Best approach for one view to call a tabbed view on iPhone


EDIT Let me simplify the question: Suppose your application has
two views, the home view and a tabbed
view with 3 tabs. The home view has
three standard buttons named "Tab1",
"Tab2" and "Tab3". When you click on
"Tab1", the tabbed view should be
opened with tab 1 selected and the
same goes for Tab2 and Tab3.

Any hints?

What I'm trying to do: when my iPhone app initializes, it displays a view with one button. When I click this button, it should go to a tabbed view.

The way I'm doing and it's not working (getting an uncaught exception): I started by creating a project from the view-based template. On the view controller that was auto-created, I've declared another view controller called TabbedViewController. Here's the header file:

#import <UIKit/UIKit.h>

@class TabbedViewController;

@interface DZBluePagesViewController : UIViewController {
    TabbedViewController *tabbedViewController;

@property (nonatomic, retain) TabbedViewController *tabbedViewController;



I've created a xib called TabbedView and set the class identity of the file owner to the TabbedViewController. I've also dragged a Tab Bar Controller to it.

I've added a button on the home view and here's the action that I've associated with it on the main view controller file:

- (void)viewDidLoad {

    TabbedViewController *tvc = [[TabbedViewController alloc] 
                                 initWithNibName:@"TabbedView" bundle:nil];
    self.tabbedViewController = tvc;
    [tvc release];

    [super viewDidLoad];

-(IBAction)goToTabbedView:(id)sender {
//  [self.view removeFromSuperview];
    [self.view insertSubview:tabbedViewController.view atIndex:0];

I'm getting an uncaught exception on the insertSubview call… I've done 3 or 4 tutorials (from the iPhone Development – Mark and LaMarche – book) on multiview apps so far and I thought I was ready to do one without anyone holding my hands… obviously wrong. Anybody can help?

Best Solution

To answer your simplified question

I think you need a third "root" view and view controller to be the superview of both the Home view and then the Tab Bar view.

When your app starts, the Root view will load the contents of the Home view. You can do so inside the root view controller with these lines:

homeViewController.view.frame = self.view.bounds;
[self.view addSubview:homeViewController.view];

Then, when you want to switch to the tab bar view, you would do this:

[homeViewController.view removeFromSuperview];
tabBarViewController.view.frame = self.view.bounds;
[self.view addSubview:tabBarViewController.view];

There's a catch: the viewWillAppear:, viewDidAppear: methods won't be called unless you call them yourself, so you'll have to insert those in the right places, too.

It might be easier to do as Ben Gottlieb suggests, and use a UINavigationController as the root view. (I mean root as the sense I've been using it, the navigation controller's root view will be your home view.) Then, instead of the code above, you can just call the pushViewController: method to slide the tabBarViewController in. If you do this you will want to customize the UINavigationController to hide the navigation bar, since you probably don't want it, and Apple actually does not recommend putting a tab bar inside a navigation view hierarchy.

Regarding the exception

The code you posted doesn't seem to have anything to do with the exception you posted. See this blog post for instructions on setting a global breakpoint on thrown exceptions. That way you can use the debugger to figure out what the offending line of code in your own program is.

Related Question