iOS使用presentModalSegue将数据从viewController2传递回viewController 1 [英] iOS Pass data back from viewController2 to viewController 1 with presentModalSegue

查看:136
本文介绍了iOS使用presentModalSegue将数据从viewController2传递回viewController 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有viewController1对我的viewController2进行模态segue但

I have viewController1 that makes a modal segue to my viewController2 but the


viewController2嵌入在导航控制器上

viewController2 is embebed on a navigation controller

因为我需要那里的导航栏。

because I need the navigation bar there.

我已经实现了一个协议,将数据从viewController2发送回viewController1,但它不起作用。这是我的代码:

I've implemented a protocol to send the data back from viewController2 to viewController1 but it doesn't function. Here is my code:

protocol writeValueBackDelegate {
    func writeValueBack(value: String)
}

class viewController1: UITableViewController, writeValueBackDelegate {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "SelectAddress"{
            if let dest = segue.destinationViewController as? MapAddressViewController{
                dest.delegate = self
            }
        }

    }
}

在viewController2上我有这个:

On the viewController2 I have this:

class viewController2: UIViewController{
    var delegate: writeValueBackDelegate?

@IBAction func TaskOneClick(sender: AnyObject) {
        delegate?.writeValueBack(txtAddress!.text!)
        self.navigationController?.popViewControllerAnimated(true)
    }
}

我不知道为什么但只有当我从中移除导航控制器时它才起作用我的secondviewController直接从viewController 1到viewController 2的segue,但我需要导航控制器来显示导航栏。

I dont know why but it functions only if I remove the navigation controller from my secondviewController and make the segue from viewController 1 to viewController 2 directly, but I need the navigation controller to show the navigation bar.

你知道为什么会这样吗?或者为什么我做得不好。

Do you know why it is happening? or Why I'm doing bad.

推荐答案

以下是我对你的设置的理解。

Here is my understanding of your setup.

ViewController1 - > NavigationController - > ViewController2

ViewController1 -> NavigationController -> ViewController2

在这种情况下,在准备segue方法中,目标viewcontroller是Navigation Controller和Not ViewController2。
因此,这行代码不会成立。

In this case, in the prepare for segue method, the destination viewcontroller is the Navigation Controller and Not ViewController2. Therefore, this line of code will not be true.

if let dest = segue.destinationViewController as? MapAddressViewController{
            dest.delegate = self
        }

你在那里的沮丧将失败,因为目标VC不是MapAddressViewContoller,而是它的UINavigation控制器。

The downcast you have there will fail, because the destination VC is not MapAddressViewContoller,instead its a UINavigation controller.

要解决此问题,您可以按如下方式更改代码:

To fix this, you can change the code as follows :

if let dest = segue.destinationViewController as? UINavigationController{
            dest.rootViewController.delegate = self
        }

但是,我更喜欢使用NSNotification将数据传回视图控制器层次结构。
您也可以试试。

However, I prefer using NSNotification to pass data back down the view controller hierarchy. You could try that out also.

使用通知


  • 步骤1:在VC1中:注册通知。
    覆盖func viewDidLoad(){NSNotificationCenter.defaultCenter()。addObserver(self,selector:Selector(myFunc:),name:SomeName,object:nil)} func myFunc( theNotifiction:NSNotification){print(我收到通知(theNotifiction.userInfo!))}


    • 第2步:在VC2中:在适当的时间发布通知。
      NSNotificationCenter.defaultCenter()。postNotificationName(SomeName,object:nil,userInfo:[theKey:theData])

    • Step 1 : In VC1 : Register for a notification. override func viewDidLoad() { NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("myFunc:"), name: "SomeName", object: nil) } func myFunc(theNotifiction : NSNotification){ print("I got notified (theNotifiction.userInfo!)") }
      • Step 2 : In VC2 : Post a Notification at an appropriate time. NSNotificationCenter.defaultCenter().postNotificationName("SomeName", object: nil, userInfo: ["theKey":"theData"])

      这篇关于iOS使用presentModalSegue将数据从viewController2传递回viewController 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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