如何在Swift中取消初始化变量引用

时间:2015-01-11 18:15:03

标签: ios swift uibutton

我有一个接受发件人的@IBAction函数:UIButton!作为参数。

@IBAction func buttonPress(sender: UIButton!)

在函数的某个时刻,我将发送者复制到另一个先前声明为UIButton()的变量

anotherVar = sender

我理解这是对原始发件人的引用,因为UIButton是一个类

然而,在代码的某些时候,我想打破对发送者的引用和"重置"另一个到普通的香草UIButton()。我该怎么做?

修改 我觉得我应该扩展我正在做的事情,也许我会以错误的方式解决这个问题......

我有八个按钮都调用了名为buttonPress()的@IBAction函数。想法是让用户点击按钮,查看图像然后点击另一个按钮(其余七个按钮中)以找到匹配的图像。当buttonPress()被调用时,代码: 1.检查这是否是第一个被点击的按钮 - 如果是,则显示按钮图像,然后将发件人分配给anotherVar; - 如果是第二个按钮被按下(即先前点击了另一个按钮),则代码运行匹配以将发送者的图像与上面设置的另一个图像进行比较 如果有匹配,我会锁定"按钮使得如果用户再次点击按钮则不执行匹配逻辑 如果没有匹配,我想"清除" anotherVar准备好进行另一个匹配任务。我不想"锁定"按钮可能仍然需要点击相同的按钮。

这里是完整的代码:

@IBAction func buttonPress(发件人:UIButton!){

    var buttonImage = UIImage()
    buttonImage = UIImage(named: listOfImages[sender.tag])!

    if (!imageIsDone[sender.tag] && (sender.tag != buttonToCompare.tag)) {
        // Only execute button logic if match for image not already found and the user isn't tapping the same image
        if (imageAwaitingCheck) {
            // User has made their first image selection, do matching logic on image clicked
            sender.setImage(buttonImage, forState: .Normal)
            if (sender.currentImage == buttonToCompare.currentImage) {
                // Tapped image macthes previously clicked image
                println("Match")

                // "Lock" the buttons as they've been matched
                imageIsDone[sender.tag] = true
                imageIsDone[buttonToCompare.tag] = true
                imageAwaitingCheck = false  
            }
            else {
                // Tapped image does not match previously clicked image
                println("No match")
                imageAwaitingCheck = false
                buttonToCompare.tag = 100 
                // ********ERROR IS HERE*********I forced this so that
                // (sender.tag != buttonToCompare.tag) is true above when
                // the user taps on the first button again after no match is found.
                // However, this is a REFERENCE to the original sender and sets the
                // button tag to 100 which causes the condition to fail and hence
                // tapping button 1, then button 2, no match, then clicking button 1
                // again doesn't execute any of this logic
            }
        }
        else {
            // User has selected this as the first image, simply show it
            sender.setImage(buttonImage, forState: .Normal)
            imageAwaitingCheck = true
            buttonToCompare = sender // I am copying sender to buttonToCompare. Ideally this would create a copy but because UIButton is a class, this is creating a buttonToCompare as a reference
        }

    }

}

1 个答案:

答案 0 :(得分:0)

只要将anotherVar指定为var,而不是let,就可以在完成sender时完成以下操作:

anotherVar = UIButton()

这将“覆盖”anotherVar之前的值,并将其“重置”为UIButton的新实例。

但是,如果您访问anotherVar的唯一地方就是此功能,您可能根本不需要这样做 - 只要您拨打anotherVar = sender,这也将取代参考使用新的发件人按钮到上一个按钮。


要在编辑后实现您所描述的内容,您实际上不需要进行大量更改。在此if语句中:if (sender.currentImage == buttonToCompare.currentImage)在最后添加buttonToCompare = UIButton()。在else - 语句中,执行相同操作而不是更改标记。

或者,如果您希望复制发件人,可以这样做:

let archivedData = NSKeyedArchiver.archivedDataWithRootObject(button)
let buttonCopy = NSKeyedUnarchiver.unarchiveObjectWithData(archivedData)