gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:26:21 26 4
gpt4 key购买 nike

我在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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com