Reputation: 33683
Let's say I have a tabbar application that nests a lot of uitableviewcontrollers and uiviewcontrollers. What is the line of code that i would use to get a handle on a particular controller?
For example, let's say I want to grab the uitableviewcontroller which is currently the 3rd tab of the tab bar controller. I am in the MyObject.h/m file which has the following code:
MyObject.h
#import <Foundation/Foundation.h>
#import "MyAppDelegate.h"
@interface MyObject : NSObject
-(void) myfunction;
@end
MyObject.m
#import "MyObject.h"
@implementation MyObject
-(void) myfunction {
UITableViewController * mytvc = /*Some line of code*/;
}
@end
What do i replace /*Some line of code*/
with such that mytvc is a variable that points to the uitableviewcontroller i'm interested in?
I am using XCode 4.2. I have a storyboard that illustrates each of my uiviewcontrollers. The Tabbar view controller was the first thing xcode made for me. So I think that means it's the top of the app?
Upvotes: 2
Views: 284
Reputation: 42588
rootViewController.tabBarController.viewControllers
is an array of the root view controller for each tab. You can set the active view controller by setting tabBarController.selectedViewController
or tabBarController.selectedIndex
.
From the app delegate it's self.window.rootViewController.tabBarController
. From a view controller it's self.view.window.rootViewController.tabBarController
.
[[self.view.window.rootViewController.tabBarController.viewControllers objectAtIndex:2] myFunction]
would access your function from another view controller. That is assuming your third tab is not a navigation controller.
If your tab has a navigation controller in it, then you have to add the navigation controller syntax on top of it. [[[self.view.window.rootViewController.tabBarController.viewControllers objectAtIndex:2] navigationController].visibleViewController myFunction]
would be the full path. Looking at it now it seems a bit ridiculous.
Upvotes: 1
Reputation: 726479
If it is your root view controller, and you have access to your app delegate, you should be able to access your controller like this:
appDelegate.window.rootViewController
Upvotes: 1