如何通过段更改自动更改按钮文本

How do I change button text automatically with a segment change

我想为用户创建一个登录屏幕,所以当他们在分段控件的 "log in" 段时,我希望它在我的按钮上显示 "log in"。但是,当用户切换到 "Sign in" 段时,我希望按钮显示 "sign in" 而不是 "log in"。到目前为止,我让它显示 "sign in" 的唯一方法是让用户单击按钮,并使用 actionButton.setTitle("sign in", for: .normal) 更改文本,但我希望它在用户切换时自动发生分割。有人知道怎么做吗?

This is what my view controller for the sign up page looks like

override func viewDidLoad() {
    super.viewDidLoad()
    segment.addTarget(self, action: #selector(valueDidChange(segment:)), for: .valueChanged)
}

func valueDidChange(segment: UISegmentControl) {
    switch segment.selectedIndex {
    case 0:
        actionButton.setTitle("log in", for: .normal)
    default:
        actionButton.setTitle("sign in", for: .normal)
    }
}