Fetching the value from textField
how to fetch value from TextFi开发者_JAVA百科eld dynamically in iphone xcode
create IBOutlet UITextfield *MyTextfield;
in your class... connect it to the textfield that you have created in Interface builder (i guess you're using IB...)
you can then fetch value of Textfield by
MyTextfield.text
anywhere in your code
The class UITextField has a property named text, so you can do something like:
UITextField* textfield = // .. get access to object somehow NSString* text_of_textfield = [textfield text];
Objective-C adds some syntactic sugar on top of properties, though, so it is more concise to use:
UITextField* textfield = // .. get access to object somehow NSString* text_of_textfield = textfield.text; // same as [textfield text]
To manually instantiate a textfield without using interface builder (although, IMHO, that's the best way):
UITextField* field = [[UITextField alloc] init]; field.text = @"Initial value"; // can replace with whatever value you want
Note that you have to release or autorelease the textfield as is appropriate.
If you have created your textfield programmatically you have to keep a reference to the textview in order to access its value.
For example you use an instance variable of the viewController which keeps the reference to the textfield, which you set when you create your textfield.
精彩评论