如何在解散时从模态视图控制器传回数据

时间:2016-10-19 08:59:03

标签: ios swift

我遵循了here的说明,但我仍然不确定这一部分:

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)
    }

4 个答案:

答案 0 :(得分:23)

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

例如,您拥有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)
    }

}

答案 1 :(得分:1)

您需要在dismissViewController方法

中调用委托方法
@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
        delegate?.sendData(typeState: "top", categoryState: "casual")
        self.dismiss(animated: true) { 
            //
        }
 }

在你的Modal ViewController类中创建委托

var delegate: MyProtocol?

使用MyProtocol中的方法名称sendData创建一个协议,并在presentViewController中为您指定委托,实现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
    }
 }

答案 2 :(得分:0)

我正在使用tabbar,因此工作代码如下

            if let tabBar = self.presentingViewController as? UITabBarController {
            let homeNavigationViewController = tabBar.viewControllers![0] as? UINavigationController
            let homeViewController = homeNavigationViewController?.topViewController as! HomeController
            homeViewController._transferedLocationID = self.editingLocationID!
        }

答案 3 :(得分:0)

如果使用导航控制器,则必须首先抓住UINavigation Controller,然后从导航控制器堆栈中获取正确的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)
}