Reputation: 21
I have been trying to change the button title to "Thank you, name". The name part it will come from the user input in a Textfield
. I Tried converting the Textfield
to a constant but when I run the app the button title display <UITextField: 0x...x7f8b29f11060>
. Any help will be appreciated.
@IBAction func sayHello(sender : AnyObject){
let greeting = "Hello, " + textField.text! + "!";
label.text = greeting;
textField.resignFirstResponder();
let name = textField!
button.setTitle("Thank you \(name)", forState: .Normal)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder();
return true;
}
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Upvotes: 0
Views: 796
Reputation: 16827
You want name to be textField.text! not textField: let name = textField!.text
For better writing and consistence, do this code:
let name = textField.text!
let greeting = "Hello, \(name)!";
label.text = greeting;
textField.resignFirstResponder();
button.setTitle("Thank you \(name)!", forState: .Normal)
Upvotes: 2