Home>

I'm making an app settings screen.
What I want to do
①In the initial display, the button is disabled and the "Save" button is grayed out
(2) The "Save" button is enabled only when there is a change in the settings (tab setting or budget setting) (*), and the button color turns orange
* "When there is a change" = Switch ON/OFF of tab setting, change of tab name, change of budget setting amount.

I'm having a hard time reproducing it.
What you can do on your own
-Deselect the Enabled checkbox for the "Save" button and disable it when initially displayed
-Change the button's appearance to the disabled color (gray)

After that, I'm wondering if I can do it somewhere in the If statement by saying "Enable button and change color when content changes" somewhere. , I don't know how to express "when there is a change in content".

if if the content has changed {
Enable the // button
   button.isEnabled = true
// Change the button color
   button.backgroundcolor = ~
   button.textColor = ~
   button.borderColor = ~
}


↓ Save button when disabled

  • Answer # 1

      

    If the content has changed

    This is to connect the Event when connecting the UITextField and code on the Storyboard with Editing Changed. As a behavior, when the contents oftextField.textchange, that is, when a character is entered or deleted, an Event is fired.

    If you write the above if statement in all the textFields, you will cry, for example

    func textChanged (_ text: String) {
      if text == "" {
        // Process when there is no character, that is, when nothing is written
      } else {
        // Process when there is a character
      }
    }

    Create a function like

    @ IBAction func textField (_ sender: UITextField) {
      guard let text = sender.text else
        fatalError ()
      }
      textChanged (text)
    }
    It is refreshing to do

    .