简体   繁体   English

取消模式视图但保留数据

[英]Dismissing a modal view but keeping the data

I'm trying to dismiss a modal view and return back to the view controller that was "sent" from, while keeping the data that was entered in the modal view. 我试图关闭模式视图,然后返回到“发送”的视图控制器,同时保留在模式视图中输入的数据。 If I understand correctly I need to use delegates/protocols for this but I'm having a lot of trouble understanding how to actually implement it in this situation. 如果我理解正确,则需要为此使用委托/协议,但在理解如何在这种情况下实际实施时会遇到很多麻烦。

Basically a user can call a modal view to enter some information in text fields, and when they hit save this function is called: 基本上,用户可以调用模式视图以在文本字段中输入一些信息,然后在单击保存时调用此函数:

func handleSave() {

    guard let newProductUrl = NSURL(string: urlTextField.text!) else {
        print("error getting text from product url field")
        return
    }
    guard let newProductName = self.nameTextField.text else {
        print("error getting text from product name field")
        return
    }
    guard let newProductImage = self.logoTextField.text else {
        print("error getting text from product logo field")
        return
    }

    // Call save function in view controller to save new product to core data
    self.productController?.save(name: newProductName, url: newProductUrl as URL, image: newProductImage)


    // Present reloaded view controller with new product added
    let cc = UINavigationController()
    let pController = ProductController()
    productController = pController
    cc.viewControllers = [pController]
    present(cc, animated: true, completion: nil)
}

Which calls the self.productController?.save function to save the newly entered values into core data, and reloads the productController table view with the new product. 哪个调用self.productController?.save函数将新输入的值保存到核心数据中,并使用新产品重新加载productController表视图。

However the issue I'm running into, is that the productController table view is dynamically set depending on some other factors, so I just want to dismiss the modal view once the user has entered the data, and return back to the page the modal view was called from. 但是,我遇到的问题是productController表视图是根据其他因素动态设置的,因此我只想在用户输入数据后关闭模式视图,然后将模式视图返回页面被叫。

EDIT: attempt at understanding how to implement the delegate - 编辑:尝试了解如何实现委托-

ProductController is the parent class that the user gets to the modal view from: ProductController是用户从以下位置进入模式视图的父类:

protocol ProductControllerDelegate: class {
func getData(sender: ProductController)
}


class ProductController: UITableViewController, NSFetchedResultsControllerDelegate, WKNavigationDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    weak var delegate:ProductControllerDelegate?

}

    func getData(sender: ProductController) {

}

And AddProductController is the modally presented controller where the user enters in the data then handleSave is called and I want to dismiss and return to the ProductController tableview it was called from: 并且AddProductController是模态呈现的控制器,用户在其中输入数据,然后handleSave ,我想关闭并返回到它从以下位置调用的ProductController handleSave

class AddProductController: UIViewController, ProductControllerDelegate {

override func viewDidDisappear(_ animated: Bool) {
    // error on this line
    getData(sender: productController)
}

If the sole purpose of your protocol is to return the final state of the view controller its usually easier and clearer to use an unwind segue instead of a protocol. 如果协议的唯一目的是返回视图控制器的最终状态,则通常更轻松,更清晰地使用展开顺序而不是协议。

Steps: 脚步:

1) In the parent VC you make a @IBAction unwind(segue: UIStoryboardSegue) method 1)在父VC中,使@IBAction展开(segue:UIStoryboardSegue)方法

2) In the storyboard of the presented ViewController you control drag from either the control you want to trigger the exit or from the yellow view controller itself(if performing the segue in code) on to the orange exit icon. 2)在显示的ViewController的情节提要中,您可以控制从想要触发出口的控件或黄色视图控制器本身(如果执行代码中的segue)的拖动到橙色出口图标。

your code should look like: 您的代码应如下所示:

    @IBAction func unwind(segue: UIStoryboardSegue) {
        if let source = segue.source as? MyModalViewController  {
            mydata = source.data
            source.dismiss(animated: true, completion: nil)
        }
    }

see apple documentation 请参阅苹果文档

Edit here is the hacky way to trigger and unwind from code without storyboard; 在这里进行编辑是一种无需脚本即可触发代码和从代码中解脱出来的简单方法; I do not endorse doing this: 我不赞成这样做:

        guard let navigationController = navigationController,
            let presenter = navigationController.viewControllers[navigationController.viewControllers.count - 2] as? MyParentViewController else {
            return
        }
        presenter.unwind(UIStoryboardSegue(identifier: String(describing: self), source: self, destination: presenter))

Basically you need to create a delegate into this modal view. 基本上,您需要在此模式视图中创建一个委托。 Let's say you have ParentViewController which creates this Modal View Controller. 假设您有创建此模态视图控制器的ParentViewController。 ParentViewController must implement the delegate method, let´s say retrieveData(someData). ParentViewController必须实现委托方法,例如retrieveData(someData)。

On the Modal View Controller, you can use the method viewWillDisappear() to trigger the delegate method which the data you want to pass to the parent: 在模态视图控制器上,可以使用方法viewWillDisappear()触发委托方法,该委托方法要将数据传递给父方法:

delegate.retrieveData(someData) proxy.retrieveData(someData)

If you have issues understanding how to implement a delegate you can check this link 如果您在理解如何实现委托方面遇到问题,可以检查此链接

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM