【问题标题】:unwind segue from coded button - swift从编码按钮展开segue - swift
【发布时间】:2015-11-04 08:56:51
【问题描述】:

我正在尝试使用 unwind segue 从用户必须选择一个选项的控制器返回:

在主控制器中,用户可以触摸一个按钮,将他们引导到第二个视图控制器,我在其中以编程方式构建按钮(选项)。当用户选择其中一个按钮时,我希望他们返回上一个控制器并传递按钮数据。

我找不到让 unwind segue 起作用的方法。

这是我在第二个视图控制器中的内容:

// Tile touch handler function when button pressed
    func selectedMood(sender:UIButton){
        println("touching tile")

        if(self.toShow == "mood"){
            self.mood = moodAre[sender.tag - 1]
        }
        else{
            self.action = actionAre[sender.tag - 1]
        }

        self.performSegueWithIdentifier("BackToPhraseSegue", sender: self)
    }

@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {
        if (segue.identifier == "BackToPhraseSegue") {
            let destination = (segue.destinationViewController as! PhraseController)
            destination.mood = self.mood
            destination.action = self.action
            destination.place = self.place
        }
    }

    // Data transmit between controller
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if (segue.identifier == "BackToPhraseSegue") {
            let destination = (segue.destinationViewController as! PhraseController)
            destination.mood = self.mood
            destination.action = self.action
            destination.place = self.place
        }
    }

我将控制器链接到情节提要中的退出按钮(选择 prepareForUnwind 函数),并为 unwindSegue “BackToPhraseSegue”提供了一个标识符

我错过了什么......

【问题讨论】:

    标签: ios swift segue unwind-segue


    【解决方案1】:

    查看此答案的第二部分,以确保您正确连接了 segue。

    How to wire up the exit segue so it can be called programmatically

    您要展开的函数需要在您要返回的 viewController 中。代替prepareForUnwind,您需要在前一个viewController(您要返回的viewController)中使用unwindToHere

    @IBAction func unwindToHere(segue: UIStoryboardSegue) {
        // And we are back
        let svc = segue.sourceViewController as! TheViewControllerClassYouAreReturningFrom
        // use svc to get mood, action, and place
    }
    

    【讨论】:

    • 太棒了!事实上,我试图从错误的控制器中操作......非常感谢你!我读到的关于这个主题的内容还不够清楚。
    猜你喜欢
    • 2014-10-11
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多