Detect rootcontroller in iphone sdk - objective-c

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.

Related

Changing master view and detail view from uisplitview

On my storyboard, my project begins with a split view that automatically assigns my custom UITableViewController (embedded in a navigation controller) as the detail view controller (done by relationship segue). How do I access the split view controls from my custom UITableViewController so I can change the master view controller views as appropriate?
UIViewController has a property splitViewController that is a reference to the split view controller the viewController is embedded inside. Since your table view controller is embedded inside a navigation controller, which is itself embedded inside a split view controller, you first need to get a reference to the nav controller, and then from that get its reference to the split view.
So in your custom tableViewController's code you can do this:
UISplitViewController *splitVC = [[self navigationController] splitViewController];
The from that you can get a reference to your masterViewController. The splitViewController has a property viewControllers which is an NSArray of two elements. The element at index zero is the master viewController. The element at index 1 is your detail view controller.
UIViewController *masterVC = [[splitVC viewControllers] objectAtIndex:0];
Note that if your master is a custom viewController subclass (which it probably is) you should cast it as such when you pull it out of the array.
If you want to relace the master view controller with a new viewController entirely, you can do that by creating a new array with your new master VC and the existing detail view controller and assigning it to your split view controller's viewControllers property:
UIViewController *detailVC = [[splitVC viewControllers] objectAtIndex:1];
NSArray *newViewControllerArray = [NSArray arrayWithObjects:newMasterVC, detailVC, nil];
splitVC.viewControllers = newViewControllerArray;

Initializing UINavigationController in AppDelegate with IB rootViewController

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.

How to get current viewController class name?

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);

Can't push view onto stack

I have a view controller:
#interface DetailViewController : UIViewController
It displays a map and pins are dropped onto this map. When the user tap's the accessory button one of the annotation views I want another view to be pushed in front of the user.
For some or other reason the navigation controller is always null when I run the following code.
hotelDetailViewController = [[HotelDetailViewController alloc] initWithNibName:#"HotelDetailViewController"
bundle:nil];
if (![self navigationController])
{
NSLog(#"navigation controller null");
}
[self.navigationController pushViewController:hotelDetailViewController animated:YES];
What am I doing wrong? At what point to do I need to alloc and init the navigation controller because it seems to be read only?
At what point to do I need to alloc and init the navigation controller because it seems to be read only?
Well, you don't usually set the navigationController property yourself, you would typically have a navigation controller set up from the start and then pass your DetailViewController to the navigation controller, and that's when the property is set.
The section in the View Controllers programming guide about Navigation Controllers explains how you should set up your navigation controller, either with a nib file or programmatically.

Access viewcontroller which resides inside a Tab Bar

I'm trying to access it in my AppDelegate by doing this
Course *rootController = (Course *)[navigationController tabcontroller];
but it won't work doesnt seem to get the "Course" root controller.
Thanks
It is not clear from your question how you have your views arranged and what you are trying to achieve. Usually a UITabBarController contains an array of root view controllers. Each view controller corresponding to a tab on the tab bar. Any or all of those view controllers could be a UINavigationController which itself can contain a stack of view controllers.
UITabBarController
|-UINavigationController -> [AViewController,.....]
|-UINavigationController -> [AnotherViewController,.....]
|-UINavigationController -> [AndAnotherViewController,.....]
The navigation controllers which in this case would be the root view controller for each tab can be accessed via the UITabBarController viewControllers property:
NSArray *rootViewControllers = [tabBarController viewControllers];
So if you want the root view controller of the first tab bar:
UINavigationController *rootViewController = [rootViewControllers objectAtIndex:0];