如何在条件展开segue中传递数据? [英] How to pass data in conditional unwind segue?

查看:120
本文介绍了如何在条件展开segue中传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试构建一个rss阅读器。在添加Feed页面上,如果点击添加按钮,我希望检查是否成功添加了Feed。如果已添加,则触发展开segue,然后返回主页面。如果未添加,请保留当前页面。

I try to build an rss reader. On the "adding feed" page, if I tap the "add" button, I hope to check if the feed is successfully added. If it is added, then trigger the unwind segue, and back to the main page. If it is not added, stay in the current page.

我知道我可以在添加按钮上建立IBAction,并检查是否添加了Feed。但是,为了添加Feed,我需要满足两个要求。

I know I can build an IBAction on the "add" button, and check if the feed is added. However there are two requirements I need to meet in order to add a feed.

首先,在我解析url后,我需要知道解析结果是否可以生成一个feed。要解析url,我需要使用mainViewController中定义的方法。

First, after I parse the url, I need to know if the parse results can generate a feed. To parse the url, I need to use the method defined in the mainViewController.

其次,我需要检查Feed是否已经存在。如果此Feed已存在,请不要添加它。要检查这一点,我需要从mainViewController获取feed数据。

Second, I need to check if the feed already exists. If this feed already exists, don't add it. To check this, I need to get the feed data from mainViewController.

目前我使用prepareForSegue将数据从主viewController传递给此视图。但对于条件展开segue,我不知道如何传递数据并检查Feed是否已存在。因为只有在触发segue时才使用prepareForSegue。如果未触发segue,我无法检查条件。

Currently I use prepareForSegue to pass the data from main viewController to this view. But for the conditional unwind segue, I don't know how to pass the data and check if the feed already exists. Because prepareForSegue is used only when the segue is going to be triggered. If the segue is not triggered, I can't check the condition.

除了通过segue之外,还有其他方法可以从其他视图传递数据吗?

Besides through segue, is there any other ways to pass data from other view?

我不知道Objective-C,所以如果你能在swift中给我一些解决方案会更好。 :)

I don't know objective-C, so it would be better if you can give me some solutions in swift. :)

推荐答案

像Schemetrical所说,使用委托是访问MainViewController中方法的简单方法。

Like Schemetrical said, using a delegate is an easy way to access the methods in your MainViewController.

由于您将此标记为Swift,我还会在Swift中为您提供一个代理的小示例。

Since you tagged this as Swift, I'll also give you a small example of a delegate in Swift.

首先你创建协议:

protocol NameOfDelegate: class {     // ":class" isn't mandatory, but it is when you want to set the delegate property to weak
    func someFunction() -> String    // this function has to be implemented in your MainViewController so it can access the properties and other methods in there
}

在你的MainViewController中你必须添加:

In your MainViewController you have to add:

class MainViewController: UIViewController, NameOfDelegate {

    // your code

    @IBAction func button(sender: UIButton) {
        performSegueWithIdentifier("toOtherViewSegue", sender: self)
    }

    fun someFunction() -> String {
        // access the other methods and return it
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "toOtherViewSegue" {
            let destination = segue.destinationViewController as! OtherViewController
            destination.delegate = self
        }
    }
}

最后一步,你必须添加委托的属性,这样你就可以对话了。我个人认为这个属性是两个视图控制器之间的某种门,所以他们可以互相交谈。

And the last step, you'll have to add a property of the delegate, so you can "talk" to it. Personally I imagine this property to be a gate of some sort, between the two view controllers so they can talk to each other.

class OtherViewController: UIViewController {

    weak var delegate: NameOfDelegate?

    @IBAction func button(sender: UIButton) {
        if delegate != nil {
            let someString = delegate.someFunction()
        }
    }
}

我假设您使用segue访问其他ViewController,因为您在你的帖子。这样,您就可以与MainViewController对话。

I assumed you used a segue to access your other ViewController since you mentioned it in your post. This way, you can just "talk" to your MainViewController.

编辑:

至于展开。这也可以通过segue完成。

As for the unwind. This also can be done through a segue.


  1. add: @IBAction func unwindToConfigMenu(sender:UIStoryboardSegue){} 到您的MainViewController。

  2. 在您的故事板中, OtherViewController 顶部有3个图标。单击内部有正方形的圆形黄色以确保选中ViewController,而不是内部的某些元素。

  3. 控制拖动(或鼠标右键拖动)从同一圆形黄色内部带方形到最右边的红色方形图标。这样做会弹出一个菜单,您可以在其中选择展开segue。

  4. 单击刚刚创建的新segue。给它一个像backToMain这样的标识符

  5. 添加类似下面代码的东西到 OtherViewController

  1. add: @IBAction func unwindToConfigMenu(sender: UIStoryboardSegue) { } to your MainViewController.
  2. In your storyboard there are 3 icons at the top of your OtherViewController. Click on the round yellow with a square inside to make sure the ViewController is selected and not some elements inside.
  3. Control drag (or right mouse drag) from the same round yellow with a square inside to the most right red square icon. Doing so pops up a menu where you can select the unwind segue.
  4. Click on the new segue you just created. Give it an identifier like "backToMain"
  5. Add something similar as the code below to OtherViewController

看来我不能发布任何代码了吗? :o稍后会添加。

It appears i can't post any code anymore? :o will add it later.

这篇关于如何在条件展开segue中传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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