当我的应用程序第一次在iPad模拟器上启动时,mainVC表会加载并列出我的模型中的数据,但详细视图控制器并不是。它只加载和列出故事板中的数据。我希望列出主菜单中的第一项(例如)。
我在AppDelegate方法的一个RayW教程中看到了这个,didFinishLaunchingWithOptions,但他们并没有使用默认的拆分视图代码,而是从头开始重新创建它,并且默认代码有一些变化。他们就是这样做的:
let splitViewController = self.window!.rootViewController as! UISplitViewController
let leftNavController = splitViewController.viewControllers.first as! UINavigationController
let masterViewController = leftNavController.topViewController as! MasterViewController
let detailViewController = splitViewController.viewControllers.last as! DetailViewController
let firstMonster = masterViewController.monsters.first
detailViewController.monster = firstMonster
return true
以下是默认代码加(在空行之后)尝试将默认代码与上述方法进行协调。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let splitViewController = self.window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
let leftNavController = splitViewController.viewControllers.first as! UINavigationController
let masterViewController = leftNavController.topViewController as! MasterViewController
let detailViewController = navigationController.topViewController as! DetailViewController
let firstRecord = masterViewController.records.first
detailViewController.record = firstRecord
return true
}
在我最初的尝试中,我正在围绕错误的控制器,但上面的代码至少编译并运行,但它没有将第一条记录加载到detailVC。
这是正确的做法,我只是遗漏了一些东西,还是有其他方法可以做到这一点?
答案 0 :(得分:1)
更简单的方法是在MasterViewController的viewDidLoad方法中实现详细的VC:
override func viewDidLoad()
{
super.viewDidLoad()
startingDetailedVCData = theData
if let split = self.splitViewController {
let controllers = split.viewControllers
self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
self.detailViewController?.detailedData = startingDetailedVCData
}
}