if you do not set a text "Mark" as a default text to the textField, it is nil at the moment that view cycle is in "ViewDidLoad". When a user enters something by using the keyboard, it happens in a different cycle.
You should check if userName.text == "Mark"
when the user taps "done" button in the keyboard, or by checking in TextFields delegates while the user is typing.
to check the user input;
you can add this in viewDidLoad;
userName.addTarget(self, action: #selector(textFieldDidChange(_:)),
for: .editingChanged)
and then, in view controller;
@objc private func textFieldDidChange(_ textField: UITextField) {
print(textField.text) // so that you can see whether you can successfully connect the outlet. if nothing prints, it means that you could not connect it properly.
if textField.text = "Mark" {
loadData1()
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…