【问题标题】:Pass data with prepareForSegue使用 prepareForSegue 传递数据
【发布时间】:2017-02-18 16:51:02
【问题描述】:

我试图将数据从 viewController 1 传递到 viewController2,我有 2 个按钮和 1 个用于这 2 个按钮的 segue(因此有一个 segue 标识符),每个按钮在按下时应该显示:1 个标签显示标题和 1 个 textView为了显示定义,我很难显示每个单词自己的数据;我知道它必须是在 performSegueWithIdentifier 中引用 SENDER 的一些代码,但我不知道该怎么做。

感谢您的帮助!!!谢谢。

这是我的代码

class ViewController: UIViewController {

    @IBAction func AbstractionBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Abstraction")
    }

    @IBAction func binarySystemBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Binary System")

    }

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "ShowDefinition") {
            if let destinationViewController = segue.destinationViewController as? EnglishViewController {
                destinationViewController.titleMsg = "Abstraction"
                destinationViewController.definitionMsg = "Abstraction Definition"
            }
    } else if(segue.identifier == "ShowDefinition"){if let destinationViewController = segue.destinationViewController as? EnglishViewController {
            destinationViewController.titleMsg = "Binary System"
            destinationViewController.definitionMsg = "Binary System Definition"
            }
        }    
}

【问题讨论】:

  • 您想在 prepareForSegue 方法中做些什么?
  • 我希望在按下 vc1 中的“抽象按钮”时显示抽象的标题和定义,当按下“二进制系统按钮”时显示其标题和定义,我正在字典中工作。跨度>

标签: ios swift swift2 sender


【解决方案1】:

您已在 performSegueWithIdentifier 的 sender 参数中正确地将定义作为字符串传递。您只需要在 prepareForSegue 中使用它的值,但首先您必须从 AnyObject 中转换它?回到字符串。

您的代码可能类似于:

class ViewController: UIViewController {
    @IBAction func AbstractionBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Abstraction")
    }

    @IBAction func binarySystemBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Binary System")
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "ShowDefinition") {
            if let destinationViewController = segue.destinationViewController as? EnglishViewController {
                if let definition = sender as? String {
                    if definition == "Abstraction" {
                        destinationViewController.titleMsg = "Abstraction"
                        destinationViewController.definitionMsg = "Abstraction Definition"
                    } else if definition == "Binary System" {
                        destinationViewController.titleMsg = "Binary System"
                        destinationViewController.definitionMsg = "Binary System Definition"
                    }
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    试试这个

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if (segue.identifier == "ShowDefinition")
        {
            if let destinationViewController = segue.destinationViewController as? EnglishViewController
            {
                 var btn = sender as! UIButton
                if btn.tag == 1
                {
                    destinationViewController.titleMsg = "Abstraction"
                    destinationViewController.definitionMsg = "Abstraction Definition"
                }
                else
                {
                    destinationViewController.titleMsg = "Binary System"
                    destinationViewController.definitionMsg = "Binary System Definition"
    
                }
    
            }
        }
    }
    

    并像这样设置按钮标签

    AbstractionBtn.tag = 1
    binarySystemBtn.tag = 2
    

    现在像这样调用 segue

    @IBAction func AbstractionBtn(sender: AnyObject) {
    performSegueWithIdentifier("ShowDefinition", sender:sender)
    }
    

    【讨论】:

    • 你好,我应该把 Abstractionbtn.tag = 1 和 binarySystemBtn.tag = 2 放在哪里?
    • 你可以写在 viewDidLoad 方法或者你可以像 UIButton "Tag" 属性直接给故事板
    【解决方案3】:

    创建一个名为titleMsg 和definitionMsg 的变量。 在每个@IBAction 方法中,将适当的标题消息设置为self.titleMsg,同样为self.definitionMsg。

    在那次通话之后,

    performSegueWithIdentifier("ShowDefinition", sender: self)
    

    然后覆盖,

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "ShowDefinition" {
            let destinationVC = segue.destinationViewController as? EnglishViewController
            destinationVC.titleMsg = self.titleMsg
            destinationVC.definitionMsg = self.definitionMsg
        }
    }
    

    希望有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      • 2012-02-06
      • 2015-09-18
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多