Reputation: 739
I have a @IBAction function that accepts a sender: UIButton! as a parameter.
@IBAction func buttonPress(sender: UIButton!)
At some point in the function, I am copying the sender to another variable which was previously declared as a UIButton()
anotherVar = sender
I understand this is a reference to the original sender given UIButton is a class
However at some point of the code, I want to break the reference to sender and "reset" anotherVar to a plain vanilla UIButton(). How would I do this?
EDIT: I feel I should expand on what I'm doing, perhaps I'm going about this the wrong way...
I have eight buttons all calling the same @IBAction function called buttonPress(). The idea is for the user to tap a button, see an image and then tap another button (out of the remaining seven) to find the matching image. When buttonPress() gets called, the code: 1. Checks to see if this is a first button being tapped - if it is, it shows the button image and then assigns sender to anotherVar; - if it is the second button being pressed (i.e. another button was previously clicked), the code runs a match to compare the sender's image to anotherVar's image which was set above 2. If there is a match, I "lock" the buttons so the matching logic doesn't get executed if the user taps the buttons again 3. If there is no match, I want to "clear out" anotherVar ready for another matching task. I don't want to "lock" the buttons as the same button may still need to be clicked.
Here's the full code:
@IBAction func buttonPress(sender: 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
}
}
}
Upvotes: 0
Views: 397
Reputation: 7256
As long as anotherVar
is assigned as var
, not let
, you can simply do the following when you're done with the sender
:
anotherVar = UIButton()
This will "overwrite" the previous value of anotherVar
and "reset" it to a new instance of UIButton
.
However, you probably don't need to do this at all if the only place you're accessing anotherVar
is this function - as long as you call anotherVar = sender
, that will also replace the reference to the previous button with the new sender button.
To achieve what you describe after your edit, you don't really need to make a lot of changes. In this if-statement: if (sender.currentImage == buttonToCompare.currentImage)
add buttonToCompare = UIButton()
at the very end. In the else
-statement, do the same instead of changing it's tag.
Alternatively, if you want the sender to be copied, you can do that like this:
let archivedData = NSKeyedArchiver.archivedDataWithRootObject(button)
let buttonCopy = NSKeyedUnarchiver.unarchiveObjectWithData(archivedData)
Upvotes: 0