ios - 从 Split ViewController 中的 Master ViewController 转到另一个 View Controller

标签 ios swift segue uisplitviewcontroller

我有一个 SplitViewController,其中一个 UITableViewController 作为 masterViewController,一个 UIViewController 作为 detailViewController。

enter image description here

enter image description here

当用户点击一个单元格时,我需要推送到一个新的 UITableViewController。所以我从单元格添加了一个 segue 到 UITableViewController。但是 UITableViewController 被添加到 masterViewController 的堆栈中。

enter image description here

如何从 masterViewController 推送到一个全新的 UITableViewController

最佳答案

这是一个简单的例子,我如何处理这些功能(我创建了一个新的Master-Detail Application):

Storyboard: enter image description here

请注意,根 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)
        }
    }
}

重要提示:

  1. 如果您从模板创建新的 MasterDetail 应用程序,您必须断开 showDetail segue,因为它是直接链接的到单元格的 selected 回调。如果您还想保留该功能,只需再次连接它,而不是从单元本身,而是从整个 VC。能够像在我时髦的 didSelect... 方法中那样使用它,该方法在偶数行上执行 showDetail segue。

  2. Split View presentation 只会工作一次 - 我还没有实现整个 rootViewController 替换 - lldb 会提示说:Attempt to present UISplitViewController on UINavigationController which view is不在窗口层次结构中! 如果您第二次尝试这样做。但这实际上取决于您对应用的行为方式的要求。

  3. 如果您想像我在我的代码中那样呈现 Split View Controller,请将 Storyboard 中的 SplitView Controller 命名为“SplitVC”(Storyboard ID)。

关于ios - 从 Split ViewController 中的 Master ViewController 转到另一个 View Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30166124/

相关文章:

ios - 将数据从 ViewController 传递到 Viewcontroller,该 Viewcontroller 现在是 TabBarController 的一部分

Storyboard Segue 未调用 iOS 协议(protocol)方法

ios - 应用程序可以在 iPad 上自动从纵向旋转为横向,但不能在 iPhone 上

objective-c - ios sdk 在将选项卡栏附加到主窗口之前在 View Controller 中附加并播放视频文件

ios - 如何使用 NSURL 和 -openURL :? 启动应用程序

ios - 将顶部边框添加到选定的 TabBar

ios - 'NSInvalidArgumentException',原因 : '+[FIRInstanceIDCheckinPreferences preferencesFromKeychainContents:]: ' unrecognized selector sent to class '

ios - 状态栏问题上方的小线

ios - 从 UITableView 中转出后,如何根据不同的数据加载不同的 UIView?

ios - 如何使用prepareForSegue从UIButton传递字符串