user859348
user859348

Reputation: 359

Advice with Tab Bar and Nav Bar

I'd just like to clear something up..

I have an app where the Main Window UI has a Tab bar with 3 tabs (opt1, opt2, op3). Each opt has its own xib file where i've drawn their own interfaces.

In my app delegate class I have included a UITabBar *rootController, and hooked this up to my tab bar in my Main Window xib file.

Now.. In the Tab bar, I have dragged in 3 navigation controllers (1 for each opt) and inside each one I have a 1) tab bar icon, 2) navigation bar and 3) view controller.

Back in my app delegate.h class I have included code for UINavigationController *nav1, nav2, nav3..and hooked these up accordingly in IB in MainWindow.xib (TabBar->navController1, navController2, navController3).

Is this the right way to do it? Also how can I make use of these nab bars in my opt1, opt2, opt3 class files?

here is my code: app delegate.h

#import <UIKit/UIKit.h>

@class LoginViewController;

@interface myAppDelegate : NSObject <UIApplicationDelegate>
{

UIWindow *window;
UINavigationController *navigationController1, *navigationController2,     *navigationController3;
IBOutlet UITabBarController *rootController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController1, *navigationController2, *navigationController3;
@property (nonatomic, retain) IBOutlet UITabBarController *rootController;

@end

appdelegate.m

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
[window addSubview:[rootController view]]; 
[window makeKeyAndVisible];

LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil];
[self.rootController presentModalViewController:loginViewController animated:NO];

}

Then in my LoginController.m class , when the user enters correct credentials I call

[self dismissModalViewControllerAnimated:YES];

In my MainWindow.xib, I hook up my rootController to a TabBarController. In the TabBarController I have put 3 NavigationControllers inside it and linked them to 3 tabOption classes which each have their own .xib view.

The tab bar switches between the 3 option views nicely. However in 1 .xib view I have a button to open a new .xib. So in my tabOption1 class I have the following:

-(IBAction)openBook:(id)sender{

UIViewController *nextVC = [[PageViewController alloc] initWithNibName:@"PageView" bundle:nil];
[self.navigationController pushViewController:nextVC animated:YES];
}

However this does not open up my PageView.xib... I have connected it to my PageViewController class and everything too..and the button works because I've tested it with a UIDialog

Upvotes: 0

Views: 558

Answers (2)

Thomas Clayson
Thomas Clayson

Reputation: 29925

In response to your comment, that looks like a reasonable way to do what you're trying to achieve. If it works, then it works.

In response to your other issue then you can get the navigation controller object by doing this: self.navigationController

So you can "go to" a new view controller like this:

// make the view controller
UIViewController *nextVC = [[MyCustomViewController alloc] initWithNibName:@"MyCustomViewController" bundle:nil];

// push it onto the navigation stack
[self.navigationController pushViewController:nextVC animated:YES];

To add this to the click event on a button you need to create the button in interface builder and create an IBAction in your code. The IBAction might look like this:

- (IBAction)pushNextViewController:(id)sender {
  UIViewController *nextVC = [[MyCustomViewController alloc] initWithNibName:@"MyCustomViewController" bundle:nil];
  [self.navigationController pushViewController:nextVC animated:YES];
}

Then you need to link to it from interface builder. I'm not sure how to do this, I generally don't use interface builder, and certainly haven't used it since about XCode 3.

To do it programatically you can use this method:

[MyButton addTarget:self selector:@selector(pushNextViewController:) forControlEvents:UIControlEventTouchUpInside]; // always use touch up inside

Keywords to look up to help you find tutorials and stuff on the internet: ibaction uinavigationcontroller pushviewcontroller:animated: popviewcontrolleranimated:

Upvotes: 0

Simon Withington
Simon Withington

Reputation: 1485

Have you seen the Apple Programming Guides? They might give you a better understanding of how everything ties together - you could start here:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/NavigationControllers/NavigationControllers.html#//apple_ref/doc/uid/TP40007457-CH103-SW1

In answer to your question, that looks like an OK way of setting up. I really would recommend reading up a bit though :)

Upvotes: 1

Related Questions