16

I've tried various methods, which all give me warnings. Such as userName = [NSString stringWithFormat:@"%@", [yournamefield stringValue]]; which just gives me a warning of

'UITextField' may not respond to '-stringValue'

How do i do this?

2

4 Answers 4

39

Get the text inside the text field using the text property

NSString *name = yourNameField.text;
5
  • No! Do not use the accessor method for something that is explicitly defined as a property in the public header.
    – PeyloW
    Jan 11, 2011 at 17:38
  • @PeyloW Can I quickly ask why this is not to be done? Isn't that what a property s doing, generating accessors? Won't the runtime convert the dot syntax to the accessor syntax?
    – user544565
    Jan 16, 2011 at 9:24
  • @MattM: Yes it is what the implementation is doing, but you should only think of that as an implementation detail and a side effect of backwards compatibility to Objective-C 1.0. Properties are intended for state, and methods are for behavior. Accessing a property as a method compiles, but your code clearly tells that you misunderstand the APIs intention.
    – PeyloW
    Jan 18, 2011 at 10:47
  • 5
    I'm afraid I don't agree with you here. Apple's own docs say that the dot syntax is syntactic sugar provided as an alternative to square bracket notation. Admittedly it does have the advantage of the compiler signalling an error when it detects an attempt to write to a read-only declared property, whereas square brackets at best generate an undeclared method warning. However, it certainly does not imply a misunderstanding of the APIs intention.
    – user544565
    Jan 18, 2011 at 11:49
  • @PeyloW Please, look at NSDateComponents and tell me if has state or behavior. Your argument is wrong. Property is term not strictly related to @property syntax. Even in Objective-C 1.0 you used properties, but you had to to write accessors yourself. Dot syntax is just easier way to invoke methods without arguments, but should not be abused for clearly behavioral methods. But this is just styling issue and it works exactly the same. It's not implementation detail. --- Ah, again an old question, that appeared on SO main page :(
    – Tricertops
    Jul 17, 2013 at 6:19
11

Use the text property of UITextField:

NSString *userName = yourNameField.text;
1

How about:

userName = [NSString stringWithFormat:@"%@", yournamefield.text];
2
  • Why use stringWithFormat: if the string the only content of the format string?
    – PeyloW
    Jan 11, 2011 at 17:37
  • No reason - it was a quick change from his original post :).
    – badgerr
    Jan 11, 2011 at 21:00
1

Two ways. It is a property and any property value can be accessed like this -

  • yournamefield.text
  • [yournamefield text]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.