Button text not changing when called in viewController init

Related searches

I'm trying to change the text of a UIButton based on values that are set in my viewController init.

So here's the init:

init(viewModel: ViewModel, stateMachine: StateMachine) {
    self.viewModel = viewModel
    self.stateMachine = stateMachine
    super.init(nibName: nil, bundle: nil) 
    title = "TEST"

The button setting method:

private func setInitialButtonText() {
    if viewModel.order.measurement.total != nil {
            button.title = "Review"
        } else {
            button.title = "Check"

Required values are loaded in viewModel.

Now when I call this button method in this init, it has no effect. Yet when I call it in viewDidLoad, it works.

The button code is as follows:

lazy var button: UIButton = {
    let button = UIButton(title: "Test", color: UIColor.green)
    button.alpha = 1
    return button

init(@escaping buttonAction) {
        super.init(frame: .zero)
        translatesAutoresizingMaskIntoConstraints = false

        button.pressedAction = buttonAction

When I put the method into init, the default initialised value of 'Test' shows on the button. However, when I print the button title from both init and viewDidLoad, first the viewDidLoad one is called and shows 'Test' and later the init one is called and shows the correct value. If this is called after viewDidLoad, why does it not reflect on the UI?

When I insert the method into viewDidLoad, the correct value is displayed...

Sure I'm missing something obvious here but any help would be appreciated.

UPDATE: I printed from viewDidDisappear as well and the correct value is printing even when I call the method from init. So the property is being set but not displayed on the button...

Not sure whether that's the problem. But have you tried to set the title of the button by using button.setTitle("myTitle", for: .normal)?

Button Action not called button.ad…, I do see that the initialization function is called, and so I know that button. Other subViews within the stackview are working correctly, specifically, there is an action for another button "Set Default Label Text", but in this case, the get errors as it appears that viewDidLoad() is not a member function of the UIView class, and� You could return a Text, Button, Image, the problem is that we are trying to change that initial @State property called offset while the View is in Set the Class to ViewController and the

None of the code above shows you actually adding the button to the view controller's view - without seeing more it's difficult to tell, but is it possible that you are also creating and adding the button in a xib, and assigning it to the button property? This would replace the button you have created in the lazy load.

Alternatively, is there a totally separate button that you are creating, but is never getting updated?

How does button get added to the view controller's view?

UIViewController, A nib file lets you specify the views of a single view controller but does not let you To initialize a view controller object using a nib file, create your view When the visibility of its views changes, a view controller automatically calls Returns a bar button item that toggles its title and associated state between Edit and Done . CHANGING BUTTON TEXT Depending on how you declare the button, you need to either use innerHTML, or the childNodes, or the value property of the button. Note: this tutorial has been tested with IE6 and Netscape 6.2 and 7, Firefox 1.04, and Opera 8 on the PC.

In the end I've added a new argument to the init as follows:

init(@escaping buttonAction, buttonTitle: String) {
        self.buttonTitle = buttonTitle
        super.init(frame: .zero)
        translatesAutoresizingMaskIntoConstraints = false

        button.pressedAction = buttonAction

Then in the button variable:

let button = I6Button(title: reviewFuelSheetButtonTitle, color: UIColor.i6.green)

And then finally in the viewController I added a didSet on the viewModel:

private var viewModel: ViewModel {
    didSet {

Update the Button Text (Guided Project Light) Unit, You've now successfully set the button to change the background color of the your view controller by control-dragging the pointer from the button to the top The outlet popup with the Name highlighted and “lightButton” entered in the field. Alternate Name - One or more additional class name synonymns (in Ext JS 6.0.0 the Ext.button.Button class has an alternate class name of Ext.Button). Alternate class names are commonly maintained for backward compatibility.

How to make your user interface in code, numberOfLines = 0 notice.text = "Your child has attempted to share the following with Apple's muddied definition of MVC it is not view controller code either. of UIView called SharePromptView , then change the class of your view as you're creating your UI in code you will also need to add init(frame:) . This can be done for instance with a property. in .h @property (nonatomic) UIViewController *viewController; in your .m file @synthesize viewController; Before calling the method, set the property with anObject.viewController = self; Then, you'll be able to call [viewController finishedPost:self]; inside

View Controllers for iOS & Swift – LearnAppMaking, This lets you change the label's properties, like text , with code. In viewDidLoad () , we're changing the text label to say: “Click the button… Instead of separating Model, View and Controller, iOS's take on MVC binds the view This often results in what's called Massive View Controller, and we'll discuss� It was not possible to destroy them and release their resources. Likewise, this did not change the reality that a controller could have any number of associated views (including none). The ViewController, however, is created very early in the component’s lifecycle and is bound to that view for its entire lifetime.

Click on the ViewController.xib file to let the Interface Builder show up on the screen. First, turn off the Autolayout option (we do this to let this demo project work on iOS older than 6 - Autolayout is an iOS 6 feature): Click on the Utilities button at the Xcode toolbar to show the Utilites pane, if it is not visible. Click on the File

  • Sadly yes I did :(
  • sorry, added the init for the buttons now - and figured it out in the end so going to post an answer