是否可以使用变量来替换引用

时间:2015-03-06 20:01:02

标签: ios swift

VC1有三个按钮' Card1',' Card2'和' Card3',每个按钮都分隔到VC2,它有四个带有图像& #39;,' 2h',' 2c'和' 2d'

segue.identifiers是Card1,Card2,Card3

按钮' 2',' 2h',' 2c'和' 2d'所有展开segue并应该用来自Images.xcassets的图像替换原始的segue-caller按钮图像:' 2s',' 2h',' 2c'或者' 2d'根据VC2中选择的按钮

CardSelected 标识哪个名为unwindSegue的VC2按钮。

和展开segue-identifier分别为1,2,3,4,第5个" clear按钮segue-identifier为0。

图像在数组卡(VC1)中声明,unwindSegue-identifier用于调用索引。

是否可以合并此代码......

按原样编辑完整代码

VC1

import UIKit

class VC1: UIViewController {

@IBOutlet weak var Card1: UIButton!
@IBOutlet weak var Card2: UIButton!
@IBOutlet weak var Card3: UIButton!

let Cards: [UIImage] = [UIImage(named: "Default")!, UIImage(named: "2s")!,UIImage(named: "2h")!,UIImage(named: "2c")!,UIImage(named: "2d")!,]

var caller = ""
var Index2 = Int()

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let KVC = segue.destinationViewController as VC2
    KVC.source = segue.identifier!}

@IBAction func unwind(unwindSegue: UIStoryboardSegue){
    Card1.setImage(Cards[Index2], forState: UIControlState.Normal)
    //I want to replace Card1.setImage with (UIButton with name = "caller").setImage
    //instead of having
    //if caller = "Card1" {Card1.setImage(Cards[Index2], forState: UIControlState.Normal)} else if caller = "Card2" {Card2.setImage(Cards[Index2], forState: UIControlState.Normal)} else if caller = "Card3" {Card3.setImage(Cards[Index2], forState: UIControlState.Normal)}
}

@IBAction func text(sender: AnyObject) {
    println(caller)
    println(Index2)
}

}

VC2

import UIKit

class VC2: UIViewController {

var CardSelected = ""
var source = ""

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    CardSelected = segue.identifier!
    var Index:Int = CardSelected.toInt()!
    let DVC = segue.destinationViewController as VC1
    DVC.caller = source
    DVC.Index2 = Index

}
}

在VC1中,我想以某种方式使Card1.setImage(Cards [Index2],forState:UIControlState.Normal)为(X).setImage(Cards [Index2],forState:UIControlState.Normal)其中(X )在代码中被当前'来电者

替换

我想将UIButtons放在一个数组中并从数组中调用它,但是

let CardCaller: [UIButton] = [UIButton(Card1)!, UIButton(Card2)!, UIButton(Card3)!]  //in VC1

带来错误:' VC1.Type'没有名为' Card1'

的成员

1 个答案:

答案 0 :(得分:0)

不确定。使cards成为destinationViewController中的字典。

var CardPicked = String()
var sourceMessage : NSString = ""

class ViewController: UIViewController {
    var cards = [String: String]()
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "Back" {

    } else {
        CardPicked = segue.identifier!
        let dvc = segue.destinationViewController as ViewController
        dvc.cards[sourceMessage] = CardPicked
    }
}