ios - 在 ContainerController 中访问 UIViewController

标签 ios objective-c ipad interface-builder container-view

我有以下设置:

SplitViewController 作为 rootController。

细节部分在前

ViewController with View > ContainerView(稍后,View 将拥有 ImageView ,但这不是这里的问题)。

ContainerView 已转入(嵌入)到另一个 View Controller (NavigationController)。

这在 IB 中以图形方式表示为:

Layout in IB

现在我想从 rootController(例如 SplitViewController)访问 NavigationController。我无法在“subViews”等层次结构中向下导航。

是否有一些方便的方法来获取 NavigationController

没有 ViewController(连同 ContainerView),我可以像这样访问它:

UISplitViewController *splitViewController = (UISplitViewController *) self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
// now i have the controller, i can delegate to it or use it in any other way:
splitViewController.delegate = (id) navigationController.topViewController;

最佳答案

当您添加 NavigationController 时,您可以将它向上传递(委托(delegate)方法)到 UISplitViewController 的子类并在那里存储一个引用。

您可以添加一个 @property (MySplitViewController *) 委托(delegate); 您的 MyContentView 并将其设置为 splitviewcontroller。当 segue 被触发时,您可以执行以下操作:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowNavigationController"])
    {
        UINavigationController *controller = segue.destinationViewController;
        [self.delegate setNavigationController:controller];
    }
}

编辑:如果你想坚持你的代码,你可以这样做:

UISplitViewController *splitViewController = (UISplitViewController *) self.window.rootViewController;
UIViewController *container = [splitViewController.viewControllers lastObject];
UINavigationController *navigationController = [container.childViewControllers lastObject];
splitViewController.delegate = (id) navigationController.topViewController;

在那种情况下,您真的应该包括一些错误处理。

关于ios - 在 ContainerController 中访问 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15022319/

相关文章:

ios - UITableViewController 上是否可以有 'sticky' 页脚?

ios - 可以在不知道设备的 UDID 的情况下通过网络进行 iOS 应用程序分发吗?

iphone - 如果我转换 View 框架, subview 框架如何变化?

iphone - CGAffineTransformMakeRotation 之后的 UIImageView "squashed"

ios - Swift UITableViewCell 子类,IBOutlet UILabel 错误,如果用作普通 var 则没有错误

IOS/objective-C : Load one record from Core Data without loading table first

iphone - 在 ARC 下使用 IBOutlets 属性的有用性?

objective-c - 与返回给调用者的局部变量关联的堆栈内存地址?

iPhone/iPad 应用程序未出现在 iPad 应用程序商店中

performance - 压缩图像是否会使图像使用更少的内存?