我已按照此处的说明进行操作,但仍不确定这部分内容:
modalVC.delegate=self; self.presentViewController(modalVC, animated: true, completion: nil)
我曾尝试以编程方式实例化视图控制器,但仍然无济于事。
这是我退出模态视图控制器时的代码:
@IBAction func dismissViewController(_ sender: UIBarButtonItem) { self.dismiss(animated: true) { // } }
我正在使用情节提要通过模式视图进行筛选。
这是我希望传输回父视图控制器的数据:
var typeState = "top" var categoryState = "casual"
这是两个字符串值。
编辑:
我试图从模态视图控制器传递数据,如下所示:
@IBAction func dismissViewController(_ sender: UIBarButtonItem) { self.dismiss(animated: true, completion: nil) delegate?.sendValue(value: "success") if let presenter = presentingViewController as? OOTDListViewController { presenter.receivedValue = "test" } }
而在父视图控制器上,我这样做的是:
func sendValue(value: NSString) { receivedValue = value as String } @IBAction func printReceivedValue(_ sender: UIButton) { print(receivedValue) }
当我按下打印按钮时,我仍然无法获得任何价值。
模态视图控制器:
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) }
这是我不使用委托方法的新代码:
@IBAction func dismissViewController(_ sender: UIBarButtonItem) { self.dismiss(animated: true, completion: nil) if let presenter = presentingViewController as? OOTDListViewController { presenter.typeState = typeState presenter.categoryState = categoryState } }
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) }
根据要传递的数据,您可以在呈现视图控制器中创建一个属性,可以在关闭模式视图控制器时进行设置,从而可以省去委托。
例如,您有一个ContactsViewController,持有一个var contacts: [Contact] = []属性。当您要创建一个新的联系人时,您将提供一个模态视图控制器,该控制器具有创建新Contact对象所需的不同值。完成并希望关闭视图控制器后,可以像在代码中一样调用该函数,但在中设置属性ContactsViewController。它看起来像这样:
ContactsViewController
var contacts: [Contact] = []
Contact
@IBAction func dismissViewController(_ sender: UIBarButtonItem) { if let presenter = presentingViewController as? ContactsViewController { presenter.contacts.append(newContact) } dismiss(animated: true, completion: nil) }
如果你 不 希望使用一个委托,这是你如何去做:
在您的OOTDListViewController:
OOTDListViewController
var testValue: String = "" @IBAction func printReceivedValue(_ sender: UIButton) { print(testValue) }
在您的模态视图控制器中(我称它为PresentedViewController):
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) }
如果 要 使用委托,请执行以下操作:
在您的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) } }