ios 如何在 XCode 中获取文本字段的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11086105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:51:36  来源:igfitidea点击:

How to get a Text Field's text in XCode

objective-ciosxcodeinterface-builderuitextfield

提问by Yetik

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?

如果是这样,我如何命名我的文本字段?

采纳答案by Greg Price

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:

因此,您要做的第一件事是在与您的 xib 文件关联的视图控制器文件的 .h 文件中创建一个属性,如下所示:

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

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

接下来在 .m 文件中,您需要像这样合成它:

    @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:

现在在界面构建器控件中,单击 Xcode 4.3 屏幕左侧显示“文件所有者”的位置,按住并拖动标签并松开。然后在弹出窗口中选择您创建的属性的名称。现在在视图控制器的 viewDidLoad 方法中,您可以像这样获取您的值:

   self.alabel.text

回答by Herm

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:

正如 Greg Price 提到的,要为您的文本字段命名,您必须创建一个 Outlet。转到您的 ViewController.h 文件并为您的文本字段创建一个属性:

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

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

使用关键字,IBOutlet您可以说 Interface Builder 有一个插座可以与此属性连接。另一方面,您将使用关键字IBAction将方法连接到对象。

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

不要忘记在你的实现下综合你的属性,然后编译器会为你创建一些标准的 setter 和 getter:

@synthesize myTextField;

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

然后转到界面构建器并右键单击,File's Owner然后将一行从 myTextField 拖到视图上的 TextField。

Now you can access the text property in your code:

现在您可以访问代码中的 text 属性:

NSString *string = self.myTextField.text;

回答by ggrana

First you will need a @property called myTextField.

首先,您需要一个名为 myTextField 的@property。

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

之后,您可以获取 textfield 的文本myTextField.text。有关更多信息,请查看类参考UITextField

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

也许您想在用户输入时获取它。如果是这样,您可以查看UITextFieldDelegate

-

——

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

编辑:如果您是客观的 c 学生,可以查看一个很酷的链接,您可以在以下位置了解更多信息:UITextFields 示例

Best Regards.

此致。

回答by CBredlow

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

苹果还制作了一个很好的教程,涵盖了相同的内容,以及其他几个 iOS 初学者主题:您的第一个 iOS 应用

回答by Reyaan Roy

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

来源:Xcode 文本字段教程