1

I'm implementing a favourite button in swift for my quote application. Since I want to disable the user to be able to favourite a quote twice. I must change the text to unlike and then compare the button text mode being unlike to the button text mode being like. And then do furthermore things based on these conditions. It would look something like:

@IBAction func favour(sender: AnyObject) {
        if liketext.text == "Like"{
            liketext.setTitle("Unlike", forState: UIControlState.Normal)
     makeQuoteFavourite()
        } else if liketext.text == "Unlike" {

            liketext.setTitle("Like", forState: UIControlState.Normal)

}
}

However as many of you know, a button outlet cannot have the .text function. @IBOutlet var liketext: UIButton! How would I be able to compare button strings to normal strings? Are there other possible solutions?

3
  • You got a bit of XY problem here; there's really no absolute need to use the button text as an implicit boolean "have we liked this" flag, however tempting it might be, and adding one specifically to track that might in some respects be easier if this proves too obstinate. Commented Aug 1, 2015 at 1:04
  • @NathanTuggy What do you suggest kind sir? Commented Aug 1, 2015 at 1:06
  • I don't actually know any Swift, but algorithmically, just set aside a static/class/otherwise-lifetime-scoped boolean variable and toggle it when the button is clicked; set the text based on the current boolean value, not independently. Commented Aug 1, 2015 at 1:07

1 Answer 1

4

UIButtons simply work differently than UILabels. They have control states for one thing. And their (title) text can be set on a per-control-state basis.

So you have to use the currentTitle property:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instp/UIButton/currentTitle

or the titleForState method:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instm/UIButton/titleForState:

Now those titles are normal String objects; it's the name of the property that's different.

Sign up to request clarification or add additional context in comments.

6 Comments

@NathanTuggy is right about modeling the 'like' state in a true model, and updating the view based on the model, but then you have to learn a lot about MVC to do it right. Just be aware to write robust Apps ultimately you must master MVC or a related UI-organizing paradigm. Don't ask me any more about it here :-)
You could ask how to implement this a bit more MVCishly in a separate question. Anyway please accept answer if settled.
I see what you mean, I should rather write if somearray has some text, then button.setitle = unlike
Other way around, but yes, you get the idea. If the array already has the text (e.g. it was already favorited), you remove the item from the array, but now the user should be given the opportunity to re-like it. And vorce veptsa (nonsense mine).
thank you for your guidance, i see the error in this way of writing code, because the button would be set to like again if i reopen the app rather than being dependent on a proper condition.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.