How to Enable a Button in Different Cases in Swift

How to enable a button in different cases in swift?

You never call imageDidChange after you changed the text.

Use:

func textViewDidEndEditing(_ textView: UITextView) {
if postTextView.text.isEmpty {
postTextView.text = "Schreibe etwas..."
postTextView.textColor = UIColor.lightGray
}

imageDidChange()
}

How to enable a currently disabled button in Swift

viewWillAppear() is called every time, before your view controller loads. When you dismiss your imagePicker, and go back to presenting your original viewController that viewWillAppear() would be called again, and it would override your actionButton.isEnable = true to being false again. If I were you, I would disable the button in viewDidLoad(), which is called only once.

Enable user interaction of a button SWIFT

In iOS and OS X development you have so-called outlets that are variables pointing to UI elements. You can read more about them here.

To declare an outlet, you prefix your variable with @IBOutlet like so:

@IBOutlet weak var button: UIButton!

and then you need to connect your outlet to the button in your xib file. That can be done in several ways, but if you have declared a variable like the above, you can:

  1. go to your storyboard
  2. in the document outline hold down the control button
  3. while holding down the control button, you drag from your files owner or ViewController to the UIButton you'd like to connect to
  4. select the outlet you'd like to connect to (button in this case)

As shown here

One way to connect an outlet

Once that is in place, you are free to use enable your button (and much more) in your code, for instance:

func viewDidLoad() {
super.viewDidLoad()
button.enabled = false
}

You can read more about IBOutlets here

And here is another way to connect outlets (this is how I prefer to connect outlets actually, you define and connect in one go)

Hope that helps

Eureka Forms Swift enable and disable button

I had the same problem, the following code get the errors count if it is 0 the form is OK and proceeds to your next function if it has any errors it does nothing.

<<< ButtonRow() { (row: ButtonRow) -> Void in
row.title = "LOGIN"
}
.onCellSelection { [weak self] (cell, row) in
print("validating errors: \(row.section?.form?.validate().count)")
if row.section?.form?.validate().count == 0{
self?.loginAction(row)
}
}

Disable a Button

The boolean value for NO in Swift is false.

button.isEnabled = false

should do it.

Here is the Swift documentation for UIControl's isEnabled property.

iOS Swift 5 unable to disable button in custom view

I don't believe there is anything wrong with your code.

When you select a color other than the default for a UIButton, you also need to select a color for the Disabled state.

Select Default Text Color:

Sample Image

Change State Config to Disabled and select a Text Color:

Sample Image

Now, in Storyboard, you can see the difference when you toggle the Enabled checkbox:

Sample Image

Sample Image

(I selected Blue for Disabled just to make it clear it's not using the "default gray")

Enable a button if all the UITextFields are filled and one of them has an Int value

Connect this @IBAction to both text fields, for Editing Changed:

@IBAction func textFieldEdited(_ sender: Any) {

// reset to disabled every time a text field value changes
// will be set to enabled if data is validated
idCheckButton.isEnabled = false

// make sure idTextField has text
guard let idText = idTextField.text, idText != "" else {
return
}
// make sure targetsTextField has text
guard let targetsText = targetsTextField.text, targetsText != "" else {
return
}
// make sure targetsTextField has only numbers
guard targetsText.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil else {
return
}

// enable the button
idCheckButton.isEnabled = true

}

This will be called with every character entered in either of the fields. It:

  • first resets the button to .isEnabled = false
  • then checks to see if the id text field has text
  • then checks to see if the targets text field has text
  • then checks to see that the targets field has only numbers

If it makes it through all 3 checks, it sets the button to .isEnabled = true

swift how to get a button inside a switch statement

The action in this line:

    btnThree.addTarget(self, action: "btnThreeclicked:", forControlEvents: .TouchUpInside) //add button action

must match the name of the method you want called - which is presumably this function:

@IBAction func buttonAclicked(sender: UIButton)

so amend the line to:

    btnThree.addTarget(self, action: "buttonAclicked:", forControlEvents: .TouchUpInside) //add button action


Related Topics



Leave a reply



Submit