如何在解散时从模态视图控制器传回数据 [英] How to pass data from modal view controller back when dismissed

查看:105
本文介绍了如何在解散时从模态视图控制器传回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照此处的说明操作,但我是仍然不确定这部分:

I've followed the instructions here but I'm still unsure about this part:

modalVC.delegate=self;
        self.presentViewController(modalVC, animated: true, completion: nil) 

我是尝试以编程方式实例化视图控制器,但仍无济于事。

I've tried instantiating the view controller programmatically but still to no avail.

这是解雇模态视图控制器时的代码:

here's my code for when dismissing the modal view controller:

@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true) { 
            //
        }
    }

我正在使用故事板来模拟视图。

I'm using storyboard to segue with modal view.

这是数据我希望转回父视图控制器:

This is the data I wish to transfer back to the parent view controller:

var typeState = "top"
 var categoryState = "casual"

这是两个字符串值。

编辑:

我试图从模态视图控制器传递数据,如下所示:

I've tried to pass data from the modal view controller as shown:

@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
        delegate?.sendValue(value: "success")
        if let presenter = presentingViewController as? OOTDListViewController {
            presenter.receivedValue = "test"
        }
    }

而在父视图控制器上,我这样做:

whereas on the parent view controller I did as such:

func sendValue(value: NSString) {
        receivedValue = value as String
    }
  @IBAction func printReceivedValue(_ sender: UIButton) {
        print(receivedValue)
    }

当我点击打印按钮时,我仍然无法获得任何值。

I still couldnt receive any value when I hit the print button.

模态视图控制器:

protocol ModalViewControllerDelegate
{
    func sendData(typeState: String, categoryState: String)
}

var delegate:ModalViewControllerDelegate!

var typeState = "top"
var categoryState = "casual"
@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
        delegate?.sendData(typeState: typeState as String, categoryState: categoryState as String)

    }

父视图控制器:

class parentViewController: UICollectionViewController, ModalViewControllerDelegate {

var typeState: String?
var categoryState: String?
func sendData(typeState: String, categoryState: String) {
        self.typeState = typeState as String
        self.categoryState = categoryState as String
    }
 @IBAction func printReceivedValue(_ sender: UIButton) {
     print(typeState)
 }

编辑:

这是我的新代码,不使用委托方法:

Here's my new code without using delegate method:

模态视图控制器:

@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
        if let presenter = presentingViewController as? OOTDListViewController {
            presenter.typeState = typeState
            presenter.categoryState = categoryState
        }
    }

OOTDListViewController:

OOTDListViewController:

@IBAction func presentModalView(_ sender: UIBarButtonItem) {
        let modalView = storyboard?.instantiateViewController(withIdentifier: "filterViewController") as! ModalViewController
        let navModalView: UINavigationController = UINavigationController(rootViewController: modalView)
        self.present(navModalView, animated: true, completion: nil)
    }
@IBAction func printValue(_ sender: UIButton) {
        print(typeState)
        print(categoryState)
    }


推荐答案

根据您要传递的数据,您可以在呈现视图控制器中创建一个属性,您可以在解除模态视图控制器时设置该属性,这样您就可以自己代表。

Depending on the data you want to pass, you can create a property in the presenting view controller, which you can set when dismissing the modal view controller, so you can spare yourself the delegate.

例如,您有 ContactsViewController ,持有 var联系人:[联系方式] = [] 属性。如果要创建新联系人,可以使用创建新联系人对象所需的不同值来呈现模态视图控制器。当您完成并想要关闭视图控制器时,您可以像在代码中一样调用该函数,但在 ContactsViewController 中设置该属性。它看起来像这样:

For example, you have a ContactsViewController, holding a var contacts: [Contact] = [] property. When you want to create a new contact, you present a modal view controller with the different values you need to create a new Contact object. When you are done and want to dismiss the view controller, you call the function as you did in your code, but set the property in the ContactsViewController. It will look something like this:

@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
    if let presenter = presentingViewController as? ContactsViewController {
        presenter.contacts.append(newContact)
    }
    dismiss(animated: true, completion: nil)
}

编辑:

如果想要使用委托,这是你如何去做的:

If you don't want to use a delegate, this is how you go about it:

在你的 OOTDListViewController

var testValue: String = ""

@IBAction func printReceivedValue(_ sender: UIButton) {
    print(testValue)
}

在你的模态视图控制器中(我会称之为 PresentedViewController ):

In your modal view controller (I'll call it PresentedViewController) :

@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
    // if your OOTDListViewController is part of a UINavigationController stack, this check will probably fail. 
    // you need to put a breakpoint here and check if the presentingViewController is actually a UINavigationController.
    // in that case, you will need to access the viewControllers variable and find your OOTDListViewController
    if let presenter = presentingViewController as? OOTDListViewController {
        presenter.testValue = "Test"
    }
    dismiss(animated: true, completion: nil)
}

如果您想要使用代理,请按以下步骤操作:

If you want to use a delegate, this is how to do it:

在你的OOTDListViewController中:

In your OOTDListViewController:

protocol ModalDelegate {
func changeValue(value: String)
}

class OOTDListViewController: ModalDelegate {

var testValue: String = ""
@IBAction func presentViewController() {
    // here, you either create a new instance of the ViewController by initializing it, or you instantiate it using a storyboard. 
    // for simplicity, I'll use the first way
    // in any case, you cannot use a storyboard segue directly, bevause you need access to the reference of the presentedViewController object
    let presentedVC = PresentedViewController() 
    presentedVC.delegate = self
    present(presentedVC, animated: true, completion: nil)
}

func changeValue(value: String) {
     testValue = value
     print(testValue)
}

}

PresentedViewController 中:

class PresentedViewController {
    var delegate: ModalDelegate? 
    var testValue: String = ""

    @IBAction func dismissViewController(_ sender: UIBarButtonItem) {
       if let delegate = self.delegate {
            delegate.changeValue(testValue)
        }
        dismiss(animated: true, completion: nil)
    }

}

这篇关于如何在解散时从模态视图控制器传回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆