How to access custom UIViewController in a UISplitViewController from another class
I have a custom customUIViewController in a UISplitViewController and want to access the instance of the customUiViewController from the detailView (which is another UIViewController inside the UISplitViewController) from another class; how can I do this?
CODE SNIP (Dont worry about the syntax; it is shorten up)
myAppDelegate.m
customViewController *masterView = [[customViewController alloc] init;
UINavigationController *NVC = [[UINavigationController alloc] initWithRootViewController:masterView];
MYViewController *detailView = [[MyViewController alloc] init;
UISplitViewController *mySplit = [...];
mySplit.viewControllers = NSArray[...masterview,detailView,nil];
[window addSubView:mySplit view];
MyViewController.m
-(void) someMethod {
customViewController *myInstance = (customViewController)[self.splitViewController objectAtIndex:0]; ??
// I think this just gets the outter UINavigationController
[myInstance doSomething];
}
customViewController.m
-(void) doSomething {
}
I want to be able to get access to customViewController to call the doSomething method. Both customViewController and myViewContro开发者_StackOverflowller is inside the same UISplitViewController
UIViewControllers have a splitViewController property so try using that to get a reference:
customViewController *myInstance =
(customViewController *)[self.splitViewController.viewControllers
objectAtIndex:0];
Index 0 is the left-side view controller in the split view controller.
Edit:
If the left-side view controller is a UINavigationController, then to get the root view controller of that, do this:
UINavigationController *nc =
(UINavigationController *)[self.splitViewController.viewControllers
objectAtIndex:0];
customViewController *myInstance =
(customViewController *)[nc.viewControllers objectAtIndex:0];
If you're working with the default UISplitView that XCode makes, you need to reference the AppDelegate to get the splitView's ivar:
YourAppDelegate *del = (YourAppDelegate *)[[UIApplication sharedApplication]delegate];
UISplitViewController *split = del.splitViewController;
NSArray *vcArray = split.viewControllers;
//left is objectAtIndex:0, right is objectAtIndex:1
精彩评论