我开始使用Master-Detail Application。现在我尝试添加一个自定义View(带有简单Label的默认UIViewController),它将显示几秒钟(如加载视图),然后才能访问detail / master视图。
我现在的问题是,如果我将我的ViewController添加到AppDelegate并从NavigationController推送到它,我会得到一个空视图。
见这里:
我没有改变任何东西,只是在appdelegate中添加了一行代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
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 masterNavigationController = splitViewController.viewControllers[0] as UINavigationController
let controller = masterNavigationController.topViewController as MasterViewController
controller.managedObjectContext = self.managedObjectContext
// +++ Custom ViewController is to be displayed from here +++
navigationController.pushViewController(ViewController(), animated: true)
return true
}
我不知道问题出在哪里...
答案 0 :(得分:1)
在代码中使用此行
navigationController.pushViewController(ViewController(), animated: true)
你正在实例化一个新的空视图控制器,你可能不是你想要的。
获取对故事板的引用(即来自masterviewcontroller)以创建视图控制器并将其推送到导航堆栈。例如:
if let myVC = controller.storyboard!.instantiateViewControllerWithIdentifier(...) {
navigationController.pushViewController(myVC, animated: true)
}