将字符串传递给unwind segue和/或dismissViewController SWIFT

时间:2015-03-05 21:32:00

标签: ios swift

VC1有1个按钮 F1 ,其中> segues>到VC2有两个按钮 Qd Qs

Qd 连接到dismissViewController

Qs 与unwindSegue

相关联

我的目标:学会将 F1 的图片更改为" Qd"通过dismissViewController进行图像处理,并学会将 F1 的图像更改为" Qs"通过放松观看图像。

问题1 :为什么 Qd (VC2)未设置F1String(VC1)= F1Card(VC2)?

问题2 :如何将此应用于 Qs 的unwindSegue?

VC1(表)

import UIKit

class Table: UIViewController {
@IBOutlet weak var F1: UIButton!
var F1String:String = "Default"

override func viewDidLoad() {
    super.viewDidLoad()

    let F1Image = UIImage(named: "\(F1String)") as UIImage!
    F1.setImage(F1Image, forState: UIControlState.Normal)
}

@IBAction func unwindFromFlop(unwindSegue:UIStoryboardSegue) {

}
}

VC2(FlopPop)

import UIKit

class FlopPop: UIViewController {
var F1Card = String()

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var DVC : Table = segue.destinationViewController as Table
    DVC.F1String = F1Card
}
@IBAction func Qd(sender: AnyObject)
{
    let F1Card = "Qd"
    self.dismissViewControllerAnimated(true, completion: nil)
}
}

感谢DDD

1 个答案:

答案 0 :(得分:0)

这解决了展开部分

VC1

import UIKit

class Table: UIViewController {
@IBOutlet weak var F1: UIButton!
var message : NSString = "Default"

@IBAction func unwindFromFlop(unwindSegue:UIStoryboardSegue) {
    let F1Image = UIImage(named: "\(message)") as UIImage!
    F1.setImage(F1Image, forState: UIControlState.Normal)
    checkLabel.text = message
}
}

VC2

import UIKit

class FlopPop: UIViewController {
var F1Card = String()

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    F1Card = segue.identifier!

    let DVC = segue.destinationViewController as Table
    DVC.message = F1Card

}
}

我认为这与使用NSString传递数据而不仅仅是字符串有关。

如果有人可以帮忙解释为什么我会非常感激,并且我仍然希望学习dismissViewController的工作。