Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

One function / action / button doing multiple things (move image and add to a counter)- Swift

I have made a button which is supposed to move an image and also add to a counter.

I have nailed the moving and counting, they work fine individually. My problem is that I cannot get the button to do both, it simply does whichever of the 2 code snippets is last. For example - with this code, the button only executes changeCount.

func changeCount() {
    count += 1
    countLabel.text = "\(count) time(s)"
}

func moveImage() {
    xRandom = randomNumber(300)
    yRandom = randomNumber(300)

    image.frame.origin.x = xRandom

    image.frame.origin.y = yRandom
}

func onPress() {
    moveImage()
    changeCount()
}

@IBAction func touchButton(sender: AnyObject) {
    onPress()
}

How do I get the button to do both?

I have tried adding multiple 'touch up inside' actions, but it still does whichever is last.

I appreciate any help!

I can't really see the problem, your code should execute both functions. There must be more to it. Can you set breakpoints in Xcode within the body of both the changeCount and moveImage functions? When you click the button, it should stop execution in moveImage first, on clicking resume it should stop at changeCount.

Hey Martin, thanks for your help.

So I added the breakpoints in the code as suggested and it is clear that the moveImage code is running first and then the changeCount. But it's still only executing the changeCount function.

I thought by swapping these around I'd see a different result, whereby the code runs through changeCount first and then the moveImage, which it did, but it still only executed the changeCount function. To further test, I commented out changeCount in the onPress function -

func changeCount() {
    count += 1
    countLabel.text = "\(count) time(s)"
}

func moveImage() {
    xRandom = randomNumber(300)
    yRandom = randomNumber(300)

    image.frame.origin.x = xRandom

    image.frame.origin.y = yRandom
}

func onPress() {
    // changeCount()
    moveImage()
}

@IBAction func touchButton2(sender: AnyObject) {
    onPress()
}

to see whether or not the moveImage code would run, and it worked fine. The image moved just as expected.

It seems like have the changeCount with the moveImage makes only the changeCount code run, but moveImage works fine on it's own.

Any ideas? Is the changeCount overriding the moveImage somehow?

have you tried just calling both directly in the touchButton function? This does seem strange. On a side note in your count function when you change the label, instead of saying "time(s)" you could check with an if statement

if count == 1 {
countLabel.text = "\(count) time"
} else {
countLabel.text = "\(count) times"
}

thats completely unrelated I know

Hi Reed,

So initially I had all of the code held in the 2 functions moveImage and changeCount -

xRandom = randomNumber(300)
yRandom = randomNumber(300)

image.frame.origin.x = xRandom

image.frame.origin.y = yRandom

count += 1

if count == 1 {
    countLabel.text = "\(count) time"
} else {
    countLabel.text = "\(count) times"
}

side note: thanks for the suggestion on the time / times if statement, makes sense!

in the touchButton function, as I thought that would work fine and was the most logical, but it would only execute whichever of the 2 code blocks was last.

I also tested calling both moveImage and changeCount functions inside the touchButton function but that was just as unsuccessful, hence the further creation of the onPress function you can see in the code in the original question.

Any more ideas would be extremely appreciated.

Could you zip and upload your project somewhere or put it on GitHub, please? I don't think the error is in the code you posted. It's hard to tell, though, it could be a threading problem or sthg. else. I would really like to run and debug it with a live build. Thanks! :)

Hi Martin, of course.

Here is the address for the repository - https://github.com/tommojackson/moveImagechangeCount

Thanks for helping out!

what happens if you set

image.frame.origin.x = xRandom

directly with

image.frame.origin.x = randomCoord(300)

of course this suggestion is just magic coding as I dont actually have an understanding of what this would change or why, but it seems worth a shot.