简体   繁体   English

Swift 4 Split View Controller Detail取代Master

[英]Swift 4 Split View Controller Detail Replaces Master

I just started building an app and right now I am adding 2 Split View Controllers, in my Main.storyboard it looks like this image 我刚刚开始构建一个应用程序,现在我在我的Main.storyboard中添加了2个Split View控制器,它看起来像这个图像

在此输入图像描述

I added the following code to my Master: 我将以下代码添加到我的主人:

import UIKit

class ContactsMaster: UITableViewController {

    var ContactsDetailController: ContactsDetail? = nil
    var objects = [Any]()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        navigationItem.leftBarButtonItem = editButtonItem

        let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
        navigationItem.rightBarButtonItem = addButton
        if let split = splitViewController {
            let controllers = split.viewControllers
            ContactsDetailController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? ContactsDetail
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
        super.viewWillAppear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc
    func insertNewObject(_ sender: Any) {
        objects.insert(NSDate(), at: 0)
        let indexPath = IndexPath(row: 0, section: 0)
        tableView.insertRows(at: [indexPath], with: .automatic)
    }

    // MARK: - Segues

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showContactDetail" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let object = objects[indexPath.row] as! NSDate
                let controller = (segue.destination as! UINavigationController).topViewController as! ContactsDetail
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
                controller.navigationItem.leftItemsSupplementBackButton = true
            }
        }
    }

    // MARK: - Table View

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return objects.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        let object = objects[indexPath.row] as! NSDate
        cell.textLabel!.text = object.description
        return cell
    }

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            objects.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
        }
    }


}

And here is my Detail: 这是我的细节:

import UIKit

class ContactsDetail: UIViewController {

    @IBOutlet weak var detailDescriptionLabel: UILabel!


    func configureView() {
        // Update the user interface for the detail item.
        if let detail = detailItem {
            if let label = detailDescriptionLabel {
                label.text = detail.description
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        configureView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    var detailItem: NSDate? {
        didSet {
            // Update the view.
            configureView()
        }
    }


}

My problem is when I run my app and goto the Split View Controller and select an item in the Master, it does not goto the Detail, but instead replaces the master. 我的问题是,当我运行我的应用程序并转到拆分视图控制器并在主控中选择一个项目时,它不会转到详细信息,而是替换主控。

I have a sample app that is just the Split View Controller and I noticed in the App Delegate file of the sample app there is this code in the application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool method: 我有一个示例应用程序只是拆分视图控制器,我注意到在示例应用程序的App Delegate文件中,应用程序中有此代码(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?) - > Bool方法:

let splitViewController = window!.rootViewController as! UISplitViewController
        let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
        navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
        splitViewController.delegate = self 

And there is also this method: 还有这种方法:

func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool {
        guard let secondaryAsNavController = secondaryViewController as? UINavigationController else { return false }
        guard let topAsDetailController = secondaryAsNavController.topViewController as? DetailViewController else { return false }
        if topAsDetailController.detailItem == nil {
            // Return true to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
            return true
        }
        return false
    }

My problem with this code is that my Split View Controller is not the inital controller and my problem with the splitViewController method, I have 2 split view controllers, I can only specificity 1 of them. 我的这个代码的问题是我的分割视图控制器不是初始控制器和我的splitViewController方法的问题,我有2个分割视图控制器,我只能特异性1。 How do I get this split view controller without making it the inital controller? 如何在不将其作为初始控制器的情况下获得此分割视图控制器?

your master class must implement UISplitViewControllerDelegate. 您的主类必须实现UISplitViewControllerDelegate。 so first thing you need to do : 所以你需要做的第一件事:

class ContactsMaster: UITableViewController,UISplitViewControllerDelegate {

and override this function in your master: 并在您的主人中覆盖此功能:

func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController, ontoPrimaryViewController primaryViewController: UIViewController) -> Bool {
return true    
}

then in viewDidLoad(master class) add following cods: 然后在viewDidLoad(master类)中添加以下代码:

self.splitViewController!.delegate = self;

self.splitViewController!.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible

self.extendedLayoutIncludesOpaqueBars = true  

I think you skip many steps for configuring splitviewcontroller, if you want to understand all the way you can read one of many tutorials written for it, like: 我想你跳过很多步骤来配置splitviewcontroller,如果你想要了解所有的方法,你可以阅读为它编写的许多教程之一,例如:

http://nshipster.com/uisplitviewcontroller/ http://nshipster.com/uisplitviewcontroller/

您是否偶然忘记在故事板中为Detail VC设置ContactsDetail类?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 拆分控制器主表视图推送到主导航堆栈,而不显示详细信息 - Split Controller master table view pushes to master nav stack not detail 在拆分视图控制器中选择主视图选项卡时更改详细视图 - Change Detail View on selecting tab of Master view in split view controller 快速将数据推送到UISplitViewController的主视图和详细视图控制器 - Pushing data to UISplitViewController's master and detail view controller swift 如何更改拆分视图控制器项目的宽度大小(主视图控制器 - 详细信息视图控制器)? - How to change width sizes of split view controller items (Master View Controller - Detail View Controller)? 从拆分视图控制器的详细视图中调用主视图控制器 - Call master view controller from within detail view of split view controller 由于存在主视图控制器,所以拆分视图控制器的详细视图偏离中心 - Split View Controller's detail view off-center because of master view controller 从拆分视图控制器的详细信息视图中进行回调委派后,主视图中的强属性变为空 - Strongly Attributed Property in Master View Becomes Null After Callback Delegation from Detail View in Split View Controller iOS - 拆分视图控制器 - 如何从主视图控制器内部获取指向详细视图控制器的指针(引用)? - iOS - Split View Controller - How do I get a pointer (reference) to the Detail View Controller from inside the Master View Controller? 故事板,标签栏控制器和主视图/详细视图 - storyboards, tab bar controller and a master/detail view 主视图中的快速拆分视图导航 - swift split view navigation in master
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM