'SWIFT: Button Title changing back to original title unwarranted
So I have the following code which executes upon tapping one side or another of my segmented control:
@IBAction func logInOrSignUpIndexChanged(_ sender: UISegmentedControl) {
// Correct setup for Sign Up
if sender.selectedSegmentIndex == 0 {
confirmPasswordTextfield.isHidden = false
confirmPasswordTextfield.isUserInteractionEnabled = true
createAccountOrLogInButton.titleLabel?.text = "Create Account"
rememberMeViewTopConstraint.constant = 20
} // Correct setup for Log In
else if sender.selectedSegmentIndex == 1 {
confirmPasswordTextfield.isHidden = true
confirmPasswordTextfield.isUserInteractionEnabled = false
createAccountOrLogInButton.titleLabel?.text = "Log In"
rememberMeViewTopConstraint.constant = -34
}
}
For some reason, when I press the segmented index value of 1 (so the bottom if statement executes), the button title changes to "Log In" for a split second then reverts back to "Create Account". I tried changing the initial title of the button which of course doesn't work, and tried taking out the line in the first if statement, that changes the title to "Create Account", and the title still changes, so I know that the first if statement is not being executed again for some arbitrary reason. Any idea what's wrong?
Solution 1:[1]
Change
createAccountOrLogInButton.titleLabel?.text = "Log In"
to
createAccountOrLogInButton.setTitle("Log In",for:.normal)
same for the first if
also
Solution 2:[2]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sh_Khan |
Solution 2 | devjme |