macos 从 textField 获取 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4868825/
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
Get int from textField
提问by Tomas Svoboda
I'm new to cocoa. I create project where I have one textField and one button. I make function for button, where I start my other function and it's ok. But I need to take number value from textField as parameter for my function...like this:
我是可可的新手。我创建了一个项目,其中有一个 textField 和一个按钮。我为按钮创建功能,在那里我开始我的其他功能,没关系。但是我需要将 textField 中的数值作为我的函数的参数......像这样:
@implementation AppController
- (IBAction)StartReconstruction:(id)sender {
int RecLine = //here i need something like textField1.GetIntValue();
drawGL(RecLine);
}
@end
In IB I only create number formated text field. But I don't know how to call it from code :(
在 IB 中,我只创建数字格式的文本字段。但我不知道如何从代码中调用它:(
thanks for help
感谢帮助
回答by futureelite7
Have you connected the textfield between your code and IB? You will need to define the ivar and property in your @interface declaration, like this:
您是否已将代码和 IB 之间的文本字段连接起来?您需要在@interface 声明中定义 ivar 和属性,如下所示:
@interface BlahBlah {
UITextField *textField1;
}
@property (nonatomic, retain) IBOutlet UITextField *textField1;
...
@end
After you have declared your ivar and connected it to your text box in IB (search google to see how), you can simply call
在您声明了您的 ivar 并将其连接到您在 IB 中的文本框(搜索谷歌以查看如何)后,您可以简单地调用
[textField1.text intValue];
to get the integer value of the string in the textbox (mind you, this is quick and dirty and does not validate the input).
获取文本框中字符串的整数值(请注意,这是快速而肮脏的,并且不会验证输入)。
回答by mcFactor
you don't have to get text value first. NSTextField inherits from NSControl which has intValue method, so...
您不必先获取文本值。NSTextField 继承自具有 intValue 方法的 NSControl,所以...
-(IBAction)buttonClicked:(id)sender
{
int intVal = [textField intValue];
NSLog (@"Int value is %i", intVal);
}
回答by redhotvengeance
Have you created an IBOutlet for the textfield so you can connect it in Interface Builder? If not, then you need to add a textfield instance to your header file and make it a synthesized property. In your header:
您是否为文本字段创建了 IBOutlet,以便您可以在 Interface Builder 中连接它?如果没有,那么您需要在头文件中添加一个文本字段实例并使其成为合成属性。在您的标题中:
@interface AppController { NSTextField *myTextField; } @property (nonatomic, retain) IBOutlet NSTextField *myTextField;
And in your implementation:
在您的实施中:
@synthesize myTextField;
Then in IB, control-click drag from the textfield to "File's Owner". A popup menu will appear and you should be able to connect it to "myTextField".
然后在 IB 中,按住 Control 单击从文本字段拖动到“文件所有者”。将出现一个弹出菜单,您应该能够将其连接到“myTextField”。
To get the int value, you should be able to do:
要获得 int 值,您应该能够执行以下操作:
[myTextField.stringValue intValue]
回答by rforte
You should be able to do:
你应该能够做到:
int RecLine = [textField1.text intValue];
This is assuming your textField1 has something that can be converted to an integer.
这是假设您的 textField1 具有可以转换为整数的内容。
回答by David
To use properties you will need to change your project's base SDK to OS X 10.5 or higher. Check this menu:
要使用属性,您需要将项目的基础 SDK 更改为 OS X 10.5 或更高版本。检查此菜单:
Project > Edit Project Settings > Build > Base SDK
回答by Ibrahim M
NSString *x = self.textfield1.text;
NSString *x = self.textfield1.text;
Int recLine = [x intValue];
Int recLine = [x intValue];