本文介绍了从 UISplitViewController 应用程序中的 DetailViewController 获取 MasterViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xcode 6.1.1 中有一个标准的 SplitViewController 应用程序.我的 DetailViewController 有一个指向我的 MasterViewController 的指针会非常方便.(我不需要发送通知,但我需要从我的 MVC 中拉取当前状态机)

I have a standard SplitViewController app in Swift in Xcode 6.1.1. It would be very convenient for my DetailViewController have a pointer to my MasterViewController. (I don't need to send notifications, but I need to pull the current statemachine from my MVC)

我的想法是使用我的 DetailViewController viewDidLoad 方法从 SplitViewController 中获取 MasterViewController.

My thought was that I would use my DetailViewController viewDidLoad method to grab the MasterViewController from the SplitViewController.

在我的 DetailViewController:viewDidLoad 中

In my DetailViewController:viewDidLoad I have

(snip)

    let tVC = self.splitViewController?.viewControllers[0] as? UIViewController
    let tNav = self.splitViewController?.viewControllers[0] as? UINavigationController
    let tMVC = self.splitViewController?.viewControllers[0] as? MasterViewController

当我检查这些时,我的 tVC 和 tNav 变量已成功设置,但我的 tMVC 为零.我认为 splitViewController.viewController[0] 会直接给我我的 MasterViewController,但我得到了一个 UINavigationController.

When I inspect these, my tVC and tNav variables were successfully set, but my tMVC was nil. I thought that the splitViewController.viewController[0] would give me my MasterViewController directly, but I'm getting a UINavigationController instead.

我错了吗/有更好的方法吗?

Am I wrong/is there a better way to do this?

推荐答案

找到了.

let tNav = self.splitViewController?.viewControllers[0] as? UINavigationController
if let tMVC = tNav?.topViewController as? MasterViewController {
        masterViewController = tMVC
        stateMachine = tMVC.stateMachine
    }

这篇关于从 UISplitViewController 应用程序中的 DetailViewController 获取 MasterViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:03