[英]How to pass data from modal view controller back when dismissed
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
設置該屬性。 它看起來像這樣:
@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
if let presenter = presentingViewController as? ContactsViewController {
presenter.contacts.append(newContact)
}
dismiss(animated: true, completion: nil)
}
編輯:
如果你不想使用委托,這是你如何去做:
在您的OOTDListViewController
:
var testValue: String = ""
@IBAction func printReceivedValue(_ sender: UIButton) {
print(testValue)
}
在您的模態視圖控制器中(我將其稱為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)
}
}
如果使用導航控制器,您必須首先獲取 UINavigation 控制器,然后從導航控制器堆棧中獲取正確的 ViewController。
這是我的代碼在這種情況下的樣子。
@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
if let navController = presentingViewController as? UINavigationController {
let presenter = navController.topViewController as! OOTDListViewController
presenter.testValue = "Test"
}
dismiss(animated: true, completion: nil)
}
你需要在dismissViewController
方法中調用delegate方法
@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
delegate?.sendData(typeState: "top", categoryState: "casual")
self.dismiss(animated: true) {
//
}
}
在你的 Modal ViewController 類中創建委托
var delegate: MyProtocol?
在 MyProtocol 和您正在分配委托的 PresentingViewController 中創建一個方法名稱為 sendData 的協議,實現 MyProtocol 方法
protocol MyProtocol: class {
func sendData(typeState: String, categoryState: String)
}
class ViewController: UIViewController, MyProtocol {
var typeState: String?
var categoryState: String?
func sendData(typeState: String, categoryState: String) {
self.typeState = typeState
self.categoryState = categoryState
}
}
我正在使用標簽欄,因此工作代碼如下
if let tabBar = self.presentingViewController as? UITabBarController {
let homeNavigationViewController = tabBar.viewControllers![0] as? UINavigationController
let homeViewController = homeNavigationViewController?.topViewController as! HomeController
homeViewController._transferedLocationID = self.editingLocationID!
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.