1

I am trying to simply capture the string typed into a UITextField in a GameController class that supports a MainViewController. My code below doesn't capture it into the wordAttempt string. Maybe its something to do with the textfield delegate which I am not sure how to set... Any help very much appreciated!

Class MainViewController: UIViewController { required init?(coder aDecoder: NSCoder) { controller = GameController() super.init(coder: aDecoder) } override func viewDidLoad() { super.viewDidLoad() let gameView = UIView(frame: CGRectMake(0, 0, ScreenWidth, ScreenHeight)) self.view.addSubview(gameView) controller.gameView = gameView } 

and then

class GameController: NSObject, UITextFieldDelegate { var gameView: UIView! var writeText: UITextField! self.writeText = UITextField(frame: CGRectMake(100,100,200,50)) writeText.delegate = self writeText.becomeFirstResponder() gameView.addSubView(writeText) textFieldShouldReturn(writeText) textFieldDidEndEditing(writeText) } func textFieldShouldReturn(textField: UITextField) -> Bool { writeText.resignFirstResponder() textFieldDidEndEditing(writeText) return true } func textFieldDidEndEditing(textField: UITextField) { self.wordAttempt = writeText.text ?? "" writeText.resignFirstResponder() } 
5
  • Is it currently printing: "wordAttempt is " ? Commented Nov 30, 2015 at 21:32
  • yes it prints wordAttempt is Optional("") Commented Nov 30, 2015 at 21:42
  • So the reason for this is that at no point do you have text in your text field. What you could do is create a separate function that you call that then prints the writeText.text Commented Nov 30, 2015 at 21:45
  • i can type text into the simulator, but its not being captured either as above or in a separate function Commented Nov 30, 2015 at 21:50
  • 1
    question updated with working solution, thx to rkyr and SnarfSnarf Commented Dec 1, 2015 at 8:49

2 Answers 2

3

If you want get informed anytime user enter some characters to your UITextField you should subscribe to it changes.

// inside your GameController self.writeText = UITextField(frame: CGRectMake(100,100,200,50)) writeText.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged) writeText.becomeFirstResponder() ... func textFieldDidChange(textField: UITextField) { let inputText = textField.text ?? "" } 

And you will get text that user type in intpuText variable. Because of optional type you should unwrap it before use. And ?? "" means "if it nil put empty string to intputText, otherwise - text". This is why you see Optional("") when you output text.

If you need to know only when user stop typing consider textFieldDidEndEditing(_:) method of UITextFieldDelegate.

class GameController: NSObject, UITextFieldDeleage { ... self.writeText = UITextField(frame: CGRectMake(100,100,200,50)) writeText.delegate = self writeText.becomeFirstResponder() ... func textFieldDidEndEditing(textField: UITextField) { let inputText = textField.text ?? "" } 
Sign up to request clarification or add additional context in comments.

2 Comments

thx rkyr - I added the UITextFieldDelegate - big miss that! So thanks. And have got the textFieldDidEndEditing delegate method working. Do you know how to get the return button to call the textFieldDidEndEditing method as well? writeText.returnKeyType = UIReturnKeyType.Done doesn't seem to do it....
@richc take a look at reference to UITextFieldDelegate. it has method called textFieldDidEndEditing. I think this is what you want. And when you use .returnKeyType you just specify how it should looks like.
1

You need to have it detect that the text has changed. You could do this say with a separate button press. Or you could use the textField delegate function textFieldDidEndEditing and textFieldDidBeginEditing to detect when the text has changed. Note that you would have to set the textfield's delegate.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.