iphone - 如何从另一个类访问 UISplitViewController 中的自定义 UIViewController

标签 iphone objective-c

我在UISplitViewController中有一个自定义的customUIViewController,并且想要从另一个类的detailView(它是UISplitViewController内的另一个UIViewController)访问customUiViewController的实例;我该怎么做?

代码片段(不用担心语法;它已缩短)

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];
 }

自定义ViewController.m

-(void) doSomething {
}

我希望能够访问 customViewController 来调用 doSomething 方法。 customViewController 和 myViewController 都在同一个 UISplitViewController 内

最佳答案

UIViewController 有 splitViewController 属性,因此请尝试使用它来获取引用:

customViewController *myInstance = 
    (customViewController *)[self.splitViewController.viewControllers 
                                 objectAtIndex:0];

索引 0 是分割 View Controller 中的左侧 View Controller 。

编辑:
如果左侧 View Controller 是 UINavigationController,则要获取其 Root View Controller ,请执行以下操作:

UINavigationController *nc = 
    (UINavigationController *)[self.splitViewController.viewControllers 
                                objectAtIndex:0];

customViewController *myInstance = 
    (customViewController *)[nc.viewControllers objectAtIndex:0];

关于iphone - 如何从另一个类访问 UISplitViewController 中的自定义 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4096754/

相关文章:

iphone - 解析要发送的数据的 APNS 提供程序

iphone - 如何检测 UITextField 何时变空

iphone - 我的 NSXMLParser :( 上的更多泄漏

objective-c - 将 NSNumber 与固定值进行比较?

ios - iOS 8 中的横向模式错误 - Xcode 6

iphone - UITabbarController + ABPeoplePickerNavigationController 无法正常工作

ios - 在ios中处理远程通知接收

ios - 我不希望我的 iOS 应用程序在 3.5 英寸屏幕的 iPhone 上运行,是否可以将我的应用程序限制为仅 4 英寸显示屏?

ios - 不支持推送导航 Controller

objective-c - 为什么Swift中的字符串、数组、字典都变成了值类型