Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton#titleLabel reverts after being changed programmatically

Tags:

ios

I have an @IBOutlet which connects to a UIButton. In my code I have the following method which is executed whenever a user performs an action. I've managed to replace my code with some dummy functions to show you guys what my issue is.

To replicate this issue, I am using a subclass of UITableViewController, you can see it below:

class MyTableViewController : UITableViewController {

    @IBOutlet nextOrCreateButton: UIButton!

    var dummy = [1, 2]
    var index: Int = 0

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dummy.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCellWithIdentifier("TemplateCell") as UITableViewCell!
    }

    @IBAction func onNextClicked() {
        if dummy.count == (index + 1) {
            dummy.append(0)
        }
        index++
    }

    func updateUI() {
        if dummy.count == (index + 1) {
            print("Setting text to FOO")
            nextOrCreateButton.titleView?.text = "FOO"
            if dummy.count == 4 {
                print("Setting text to FOOBAR")
                nextOrCreateButton.titleView?.text = "FOOBAR"
                nextOrCreateButton.enabled = false
            }
        } else {
            print("Setting text to BAR")
            nextOrCreateButton.titleView?.text = "BAR"
        }
    }
}

The code above has a button that is clicked, and when it is clicked it will either move to the next dummy value, the dummy value isn't used for anything in this example, other than to show that changing the text doesn't work.

What happens is the code executes how it should (According to debug messages) but the button's text changes to what it's set to, and almost immediately changes back.

Clicking the button X times prints the following logs:

Setting text to BAR
Setting text to FOO
Setting text to FOO
Setting text to FOO
Setting text to FOOBAR

However, the button always quickly changes back to FOO, even after changing to "FOOBAR" as it says it does, the button doesn't even remain disabled as we set in the code.

like image 240
Hobbyist Avatar asked Jan 22 '16 12:01

Hobbyist


People also ask

What is a UIButton?

A control that executes your custom code in response to user interactions.

What is a Uicontrol?

The user interface domain elements are used to describe the user interface of a software program. uicontrol. The <uicontrol> element is used to mark up user interface controls, such as names of buttons, entry fields, menu items, or other objects that allow the user to control the interface.

What is Uiimage?

An object that manages image data in your app.

What is Nsbutton?

A control that defines an area on the screen that a user clicks to trigger an action. macOS 10.0+


2 Answers

Don't do this on a UIButton

nextOrCreateButton.titleView?.text = "FOOBAR"

Always use

nextOrCreateButton.setTitle("FOOBAR", forState: UIControlState.Normal)

Or swift3

nextOrCreateButton.setTitle("FOOBAR", for: .normal)
like image 81
MarkHim Avatar answered Jan 21 '23 03:01

MarkHim


Have you tried using setTitle(_ title: String?, forState state: UIControlState)?

like image 38
Tobi Nary Avatar answered Jan 21 '23 04:01

Tobi Nary