How to get a Text Field's text in XCode

断了今生、忘了曾经 提交于 2019-12-04 17:44:12

问题


I made a Text Field with interface builder. How can I get its text to use somewhere else?

Is there something like:

string text = myTextField.Text;

If so, how can I name my text field?


回答1:


So the first thing you want to do is create a property in the .h file of the view controller files associated with your xib file like so:

    @property (strong, nonatomic) IBOutlet UILabel *aLabel;

Next in the .m file you need to synthesize it like so:

    @synthesize aLabel;

Within you implementation tag.

Now in interface builder control click where it says "File's Owner" on the left side of the screen in Xcode 4.3 hold down and drag over the label and let go. Then in the popup select the name of the property you created. Now in the viewDidLoad method of your view controller you can get at your value like so:

   self.alabel.text



回答2:


As Greg Price mentioned, to get a name for your text field, you have to create an Outlet. go to your ViewController.h file and create a property for your text field:

@interface YourViewController: UIViewController
@property (nonatomic, retain) IBOutlet UITextField myTextField
@end

With the keyword IBOutlet you say the Interface Builder that there's an outlet to connect with this property. On the other hand you'll use the keyword IBAction to connect methods to the objects.

Don't forget to synthesize your property, right under your implementation, then the compiler will create you some standard setter and getter:

@synthesize myTextField;

Then go to the interface builder and right click on the File's Owner then drag a line from myTextField to the TextField on the View.

Now you can access the text property in your code:

NSString *string = self.myTextField.text;



回答3:


First you will need a @property called myTextField.

After that you can get the text of the textfield myTextField.text. For more information take a look at the class reference UITextField

Maybe you want to get it while the user is typing. If so you can take a look at UITextFieldDelegate

-

Edit: A cool link to take a look if you are a objective c student, you can learn more at: UITextFields examples

Best Regards.




回答4:


There is also a nice tutorial apple made that covers the same thing, as well as a couple other beginner iOS topics: Your First iOS App




回答5:


Its quite simple just drag Text Field from View Area to your Storyboard. Then activate assistant editor so that you can view both code window and storyboard at the same time. Then press control button and drag the Text Field from your storyboard to code window and thats it.

//created a string variable
    var name: String = ""

    //our label to display input
    @IBOutlet weak var labelName: UILabel!

    //this is the text field we created
    @IBOutlet weak var textFieldName: UITextField!

    @IBAction func buttonClick(sender: UIButton) {
        //getting input from Text Field
        name = textFieldName.text!


    }

Source: Xcode Text Field Tutorial



来源:https://stackoverflow.com/questions/11086105/how-to-get-a-text-fields-text-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!