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?
/EDITWhat 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 {
开发者_如何学JAVA TabbedViewController *tabbedViewController;
}
@property (nonatomic, retain) TabbedViewController *tabbedViewController;
-(IBAction)goToTabbedView:(id)sender;
@end
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?
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.
Open the run console (shift-command-R) when you get the exception. Post that. You may also want to open the console and, at the prompt, type "bt" for "back trace".
Post the exception.
I would suggest using a UINavigationController, and then pushing your tabController onto it once they select a button. If you don't want the slide animation, just pass NO for the animated: argument.
Why have a redundant menu view? Why not just have the tab view as a view on its own and let the user select which tab they wish to choose? I don't understand why you need a menu that just opens a particular tab when the tab bar interface does that anyway without the menu...
精彩评论