but the problem is that my app has both UInavigationController and UITabBarController
so calling navigaionController.topViewController tells me that i have UItabBarController
and
self.window.rootViewController returns UINavigationController
thank's a lot
You can check for the kind of class it is using
[VC isKindOfClass:(myVCClass class)]
The tabbarcontroller is designed to be the top/root viewcontroller of your application. From the documentation:
Because the UITabBarController class inherits from the UIViewController class, tab bar controllers have their own view that is accessible through the view property. When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.
Have the navigationcontroller inside the tabs and have the other view controllers inside the navigationcontrollers on the tabs.
view.class returns a class name as a string:
NSLog (#"Class:%#", view.class);
Related
I have using two types of controller in my application i.e. NavigationController and presentViewController.
How can I detect base controller at any instance through code i.e I am using navigation or presentviewcontroller to transist one viewcontroller to another viewcontroller?
Try this for take rootviewcontroller of navigation:
UIViewController *topViewController = [self.navigationController topViewController];
for present modal view controller check out the 'presentingViewController' property of UIViewController and for navigation you can get the array of view controllers NSArray *ArryViewControllers=[self.navigationController viewControllers]; and then get the object at index 0 . this will be the root view controller of that navigation controller.
Hello I have a navigation view controller on the Interface builder and have it placed on a window.rootviewcontroller and then I pushed the initialized view controller to it. I used this line here: [window.rootViewController addSubViews:[navigation view]];
The [navigation view] has no view inside.
But if i create a navigation in code and do the same as before, i can have the navigation appears on the screen.
Does anyone know what is wrong? i have linked the navigation with the Interface Builder as well.
Have you set the class correctly for the "viewcontroller" which is visible inside navigationcontroller(in XIB) within the navigationController hierarchy
You should try this
if you want to set RootViewController
self.window.rootViewController = navigationController;
//navigationController its is rOotViewControler.
if You want to add as SubView.
[self.window addSubview:navigationController.view];
i hope it'll help you.
I'm working in my AppDelegate to make the app's view return to the first view every time it comes from the background.
First off, I have a navigation controller set up in IB as my initial view controller.
Despite this, if I put
if (!self.window.rootViewController.navigationController)
NSLog(#"null rootview navcontroller");
in appDidFinishLaunching the NSLog happens indicating that my window's navigation controller is null.
So, I figured I would try instantiating it myself with:
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController];
But when I try that I get error:
Pushing a navigation controller is not supported
Which I must confess I don't really understand. I shouldn't be pushing anything?
Thanks!
If the navigation controller is the initial view controller then self.window.rootViewController should be the navigation controller itself.
The type for rootViewController is UIViewController, so you will need to cast as needed.
I am working on an Iphone application.
I am using a StoryBoard.
I have a Tab View with 3 tabs. "Home", "Users" and "Settings".
I create the "Home" and "Users" view on the story board, but The settings view is a XIB file (SettingsView.xib)
How can I make the third tab ("Settings") open the SettingsView.xib? Can I use both the story board and xib files?
I tried to initialize a UINavigationController in the startApp method in the AppDelegate but I can't find out how to add it to the story board.
Thanks for any help
TabViewControllers usually have one navigation controller for each tab.
Create the navigation controllers in storyboard and connect them to the navigationcontrollers relation of the tab view controller.
The initial view of the navigation controller connects to the rootViewController relationship of the navigation controller.
As to your second question, I'm not certain, but I think the following will work:-
Create a UIViewController in storyboard and change it's class to your class that you're loading from an XIB. When the storyboard instantiates the class, it will use the XIB provided the class name of the class exactly matches the name of the XIB. I don't think you can do any iPad/iPod checking here though.
You can add a xib-based view to your storyboard-based tab bar controller as follows. I am assuming the following:
The tab bar controller is the initial view controller of your storyboard.
Your settings controller is a class called SettingsController
You have a tab bar image in your bundle called SettingsTabImage
Define the tab bar controller in the storyboard with just your storyboard-based tab bar items in it - Home and Users in your case
In your application delegate, use the following code in application:didFinishLaunchingWithOptions::
// Create your settings view controller
SettingsController *settingsVC = [[SettingsController alloc] initWithNibName:nil bundle:nil];
// Create a tab bar item
UITabBarItem *settingsItem = [[UITabBarItem alloc] initWithTitle:#"Settings" image:[UIImage imageNamed:#"SettingsTabImage" tag:0];
settingsVC.tabBarItem = settingsItem;
// Get a reference to the tab bar controller
UITabBarController *tbC = (UITabBarController*)self.window.rootViewController;
// Get the current view controllers in your tab bar
NSMutableArray *currentItems = [NSMutableArray arrayWithArray:tbC.viewControllers];
// Add your settings controller
[currentItems addObject:settingsVC];
tbC.viewControllers = [NSArray arrayWithArray:currentItems];
I'm trying to connect Tab Bar Controller to existing part of my app, but when I do that it's "malfunctioning".
However when I run Tab Bar Controller part standalone as initial view controller it works properly like in the image below :
This is how app looks when it is run(correct behavior) :
However when I go to this tab bar controller from my main app this is how it looks like this:
My main app looks like this :
Scroll View contains
UIView 1
UIView 2
UIView 3
UIView x
Each view does something not related to this tab bar controller. Only one view view x tries to "visit" tab bar controller and display some data there, but it's not. Any ideas?
I have this tab bar controller identifier set to test, and I here is how I do that from my view x :
UITabBarController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"test"];
[self.view addSubview:newViewController.view];
EDIT :
Entire app :
I'm creating views programatically. That's why I don't have any relationships/segues to the tab bar controller.
SOLUTION :
Change :
UITabBarController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"test"];
[self.view addSubview:newViewController.view];
To :
UITabBarController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"test"];
[self addChildViewController:newViewController];
[self.view addSubview:newViewController.view];
You need to set a root viewController to the navigation Controller
Just don't do this. From the Apple UITabBarController referenece
Because the UITabBarController class inherits from the UIViewController class, tab bar controllers have their own view that is accessible through the view property. When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.
As I understand it means you must use UITabBarController only as a root view controller of the window. But you can alway use a general UIViewController and add UITabBar there.
(The view offset problem you've met is possible to be fixed, it will not follow the Apple guidelines however and not advised).