我有一个 SplitViewController
,其中一个 UITableViewController
作为 masterViewController,一个 UIViewController
作为 detailViewController。
当用户点击一个单元格时,我需要推送到一个新的 UITableViewController
。所以我从单元格添加了一个 segue 到 UITableViewController
。但是 UITableViewController
被添加到 masterViewController 的堆栈中。
如何从 masterViewController 推送到一个全新的 UITableViewController
?
最佳答案
这是一个简单的例子,我如何处理这些功能(我创建了一个新的Master-Detail Application):
Storyboard:
请注意,根 VC 现在是 UINavigationController
。因此 AppDelegate
必须进行相应的更改:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let navCtr = self.window!.rootViewController as UINavigationController
let splitViewController = navCtr.visibleViewController as UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as UINavigationController
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
return true
}
最后在 MasterViewController
中添加:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
if indexPath.row % 2 == 0 {
self.performSegueWithIdentifier("showDetail", sender: nil)
} else {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
if let let rootCtr = appDelegate.window?.rootViewController {
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let newSplit = storyboard.instantiateViewControllerWithIdentifier("SplitVC") as UISplitViewController
/// Because of Apple's "Split View Controllers cannot be pushed to a Navigation Controller"
let yetAnotherNavCtr = UINavigationController(rootViewController: newSplit)
rootCtr.presentViewController(newSplit, animated: true, completion: nil)
}
}
}
重要提示:
如果您从模板创建新的 MasterDetail 应用程序,您必须断开
showDetail
segue,因为它是直接链接的到单元格的selected
回调。如果您还想保留该功能,只需再次连接它,而不是从单元本身,而是从整个 VC。能够像在我时髦的didSelect...
方法中那样使用它,该方法在偶数行上执行showDetail
segue。Split View presentation 只会工作一次 - 我还没有实现整个 rootViewController 替换 - lldb 会提示说:
Attempt to present UISplitViewController on UINavigationController which view is不在窗口层次结构中!
如果您第二次尝试这样做。但这实际上取决于您对应用的行为方式的要求。如果您想像我在我的代码中那样呈现 Split View Controller,请将
Storyboard
中的 SplitView Controller 命名为“SplitVC”(Storyboard ID)。
关于ios - 从 Split ViewController 中的 Master ViewController 转到另一个 View Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30166124/