My app currently has a UINavigationController and I'd like to push a UITabBarController at some point when a button is clicked. I am trying to create it on Interface Builder (as opposed to programatically).
All online tutorials show how to create a tab bar based app, which involves dragging a UITabBarController into the MainWindow.xib which is obviously not what I want.
What I did was create a UIViewController, and its nib file, dragged a UITabBarController. Now pushing that UIViewController to the navigation controller will show an empty view (its empty view). Removing the view in the view controller will crash the app. How can I tell the UIViewController to load a UITabBarController instead of its own view?
For those down-voting me: it would be decent to at least provide a comment. The question is not a poor question. The questions is asking for suggestions for how to use a UITabBarController in an unorthodox way. I tried most of the suggestions and they do not work. If you are down-voting, at least write a comment.
You can see this this may help you
Since this is how you want your app to be: - Navigation Controller - Root View Controller - Other View Controllers - Tab Bar Controller - First VC under tab - Second VC under tab - Third VC under tab - more view controllers
in your view controller where you want to pushViewController to UITabBarController use this
//create a UITabBarController object
UITabBarController *tabBarController=[[UITabBarController alloc]init];
//FirstViewController and SecondViewController are the view controllers you want on your UITabBarController (Number of view controllers can be according to your need)
FirstViewController *firstViewController=[[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondViewController=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
//adding view controllers to your tabBarController bundling them in an array
tabBarController.viewControllers=[NSArray arrayWithObjects:firstViewController,secondViewController, nil];
//navigating to the UITabBarController that you created
[self.navigationController pushViewController:tabBarController animated:YES];
This tutorial might help. It comes with an example code.
Hi just make both nav controller and tabBar Controller in app delegate.
Initially add navController to your root view..
[self.window addSubview:navigationController.view];
and whenever you want to add tab bar then remove navController and add tabBarController.
-(void)addTabBarController
{
AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];
[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window]addSubview:[[appdelegte tabcontroller]view]];
[[appdelegte tabcontroller]setSelectedIndex:0];
}
If you get any problem then ask me again..
In YourView controller make IBOutlet of tabBarController
in .h file
#import <UIKit/UIKit.h>
#interface YourView : UIViewController
{
IBOutlet UITabBarController *tabBarController;
}
-(IBAction)loadTabBar:(id)sender;
#end
and in .m file
#import "YourView.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#implementation YourView
-(IBAction)loadTabBar:(id)sender
{
FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil];
[self.navigationController pushViewController:tabBarController animated:YES];
}
#end
The tabBarController IBOutlet must be connected to the UITabBarController that on the .xib file. And that UITabBarController with two view controllers named FirstViewController, SecondViewController.
I remember doing something similar to this...
I had to create a custom UITableViewController to do this, if you are going to use UINavigationController to 'push' to it.
Doing it only in interface builder may be a bit tricky, it's been a while since I've been at it, I do recall it was a bit of a nightmare to get going correctly.
The problem was, as I believe I've mentioned somewhere, is that the XIB does not have a UIView connected to it. When the UIView is deleted in a XIB file and a UITabBarController is added, the view property of the XIB has to be connected to the UITabBarController's view. I connected it and it worked. That was the reason why I was getting a SIGTRAP.
take a uiview of tab bar controller means create an interface builder with tabs and add that tab bar uivew in your classes where ever u wanted the tab bar
for example take a tab bar uiview of 3 tabs in that uiview take the three buttons in the interface builder
for every navigation of that classu should add this uiview class
-(IBAction)firt_button_pressed:(id)sender
{
}
-(IBAction)second_button_pressed:(id)sender
{
}
Related
I try this:
ViewController.h
#class SecondView;
#interface Introduccion : UIViewController{
SecondView *second;
}
-(IBAction)AnimatecreditsPage:(id)sender;
#end
ViewController.m
-(IBAction)AnimatecreditsPage:(id)sender{
second = [[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];
second.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:second animated:YES];
}
Im using Storyboards, and i already linked my viewcontroller with the corresponding classes, when i press the button, the iphone simulator just crash.. im using a navigation controller and tab bar controller.
Image of the viewcontroller
THANKS!! :)
Please Help Me.
The way you're trying is crashing because you're pointing to a xib that doesn't exist. Since with storyboards you can have multiple view controllers you have to add an identifier to the view controller you wish to use in the attributes inspector section of interface builder. This then allows you to use the following code to programmatically instantiate what ever view controller in your storyboard has the ID you specify.
second = [self.storyboard instantiateViewControllerWithIdentifier:#"someID"];
Instead of:
second = [[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];
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
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 :)
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:
I have a View application with a Single UIViewController. I then add a UITableViewController through the IB, and I am trying to display the UITableViewController through a button press in the UIViewController (my main view). My button press (IBAction) contains the following code through which I am trying to push my UITableViewController view and display it:
DataViewController *dataController = [[DataViewController alloc] initWithNibName: #"DataViewController" bundle:nil];
[self.navigationController pushViewController:dataController animated:YES];
[dataController release];
My DataViewController is not at all getting pushed into the stack and displayed,
Also I have checked that in the code above, self.navigationController=nil
Probably this is the source of the problem. If so, how to rectify it?
Please help.
UINavigationController *navCtrlr = [[UINavigationController alloc]initWithRootViewController:yourfirstviewController];
[self.window setRootViewController:navCtrlr];
navCtrlr.delegate = self;
navCtrlr.navigationBarHidden = YES;
Create navigation controller in appdelegate.m then you can navigate to any uiviewcontroller
You need to actually create a UINavigationController. The navigationController property tells you whether your DataViewController is currently in a UINavigationController's hierarchy; if not (as in this case), the navigationController property returns nil.
I'm trying to create a modal view which pops up when the user presses a button. The modal view has a navigation bar with a map view as the main view. I'm having trouble setting this up in Interface Builder. When I set the view outlet for my File's Owner's view to the view inside the Navigation Controller, the only thing that show up is the map view, with a grey space at the top and bottom. The navigation bar never appears. Here's a screenshop of how it looks, with an image of my IB window.
How can I get the navigation bar to show up properly? Thanks
alt text http://img.skitch.com/20100126-d5u4yuufpe77xdkuw2k1h9uahf.jpg
http://img.skitch.com/20100126-xrw6qd5jajytkq5u7x3kdk168s.jpg http://img.skitch.com/20100126-xrw6qd5jajytkq5u7x3kdk168s.jpg
Just in case, here's the MapViewController declaration:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapViewController : UIViewController
{}
#end
And the code to push the modal view controller:
MapViewController *mapVC = [[MapViewController alloc] init];
self.mapViewController = mapVC;
[mapVC release];
[self presentModalViewController:mapViewController animated:YES];
You are going about this the wrong way in your NIB file.
Add the MKMapView to the view of the UIViewController. You could add a navigation bar here instead of a controller if you don't wish this view to go anywhere else.
However, having the navigation controller is very handy. So, ensure that there is no navigation controller in the NIB file for your MapViewController class and then edit your code to look like this:
MapViewController *mapVC = [[MapViewController alloc] init];
self.mapViewController = mapVC;
[mapVC release];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mapViewController];
[self presentModalViewController:navController animated:YES];
[navController release];
Then you can access this controller from within your MapViewController.m file by using:
self.navigationController
I have a navigationController app.
I push a tabbar onto the view. Tabs are working title is changed, perfect. Now one of my tabs has a list and I try to link out to a child page within the tabbar:
NextViewController *nextController = [[NextViewController alloc] initWithNibName:#"ProfileDetailController" bundle:nil];
[self.navigationController pushViewController:nextController animated:YES];
Nothing happens. Course this works:
self.view = nextController.view;
I want to be able to push to this subpage within my tabbar AND change the navigationbars buttons. Is this possible?
It sounds like you're pushing a UITabBarController onto a UINavigationController? From Apple's documentation, you can't push a tab bar controller onto a navigation controller.
What you probably want to do is the opposite: have a tab bar controller with UINavigationControllers as the tab items. This is similar to the interface in, say the iPod app or Phone app.
I agree with Alex - a TabBarController inside a navigation controller doesn't seem like a nice UI pattern.
Anyways, to answer your question: Have you tried to access the navigation controller via the tab bar controller?
self.tabBarController.navigationController
I'm not sure if this works, but you could give it a try.
I think I found an easy solution.
In your class where you want to push a view, declare a local UINavigationController as a propterty:
#interface userMenu : UIViewController {
UINavigationController *navigationController;
}
#property (nonatomic, retain) UINavigationController *navigationController;
Remember to synthesize it.
In your class for the tabBarController:
NSArray *viewControllersArray = [self.tabBarController viewControllers];
userMenu *childUserMenu = (userMenu*) [viewControllersArray objectAtIndex:0];
childUserMenu.navigationController = self.navigationController;
After that you can do [self.navigationController pushViewController:nextController animated:YES];