1

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?

  • My problem with Xcode 13 was fixed [here](https://stackoverflow.com/a/69375669/2704776): Select Style as Default – devjme May 10 '22 at 18:58

2 Answers2

2

Change

createAccountOrLogInButton.titleLabel?.text = "Log In"

to

createAccountOrLogInButton.setTitle("Log In",for:.normal)

same for the first if also

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • This works, and I will accept it in a few minutes when SO allows me. However I have one more question. Why does the title change in a fade in fade out manner when I do this? In other words, when I switch segments, it fades to current title out then fades the new one in. I have not knowingly set it up to be that way. Thanks for your help though –  Nov 12 '20 at 21:35
  • That's just what UIKit does. You can put the `setTitle` inside a [`performWithoutAnimation`](https://developer.apple.com/documentation/uikit/uiview/1622484-performwithoutanimation) to remove the effect – Paulw11 Nov 12 '20 at 21:46
  • I am typing performWithoutAnimation all throughout my view controller and no function pops up... am I doing something wrong? Not sure how to apply this. thanks –  Nov 12 '20 at 21:55
  • 1
    @derek check https://stackoverflow.com/a/29630753/5820010 – Shehata Gamal Nov 12 '20 at 22:05
0

My issue with Xcode 13 was posted here

Select the style of default on the button.

devjme
  • 684
  • 6
  • 12