Objective c 从textField获取int
我是可可新手。 我创建了一个项目,其中有一个文本字段和一个按钮。 我为按钮创建了一个函数,在这里我启动了其他函数,一切正常。但我需要将textField中的数值作为函数的参数…如下所示:Objective c 从textField获取int,objective-c,cocoa,macos,Objective C,Cocoa,Macos,我是可可新手。 我创建了一个项目,其中有一个文本字段和一个按钮。 我为按钮创建了一个函数,在这里我启动了其他函数,一切正常。但我需要将textField中的数值作为函数的参数…如下所示: @implementation AppController - (IBAction)StartReconstruction:(id)sender { int RecLine = //here i need something like textField1.GetIntValue(); dra
@implementation AppController
- (IBAction)StartReconstruction:(id)sender {
int RecLine = //here i need something like textField1.GetIntValue();
drawGL(RecLine);
}
@end
@interface BlahBlah {
UITextField *textField1;
}
@property (nonatomic, retain) IBOutlet UITextField *textField1;
...
@end
在IB中,我只创建数字格式的文本字段。但我不知道如何从代码中调用它:(
感谢您的帮助您应该能够做到:
int RecLine = [textField1.text intValue];
[myTextField.stringValue intValue]
这是假设您的textField1具有可以转换为整数的内容。您是否为textfield创建了IBOutlet以便可以在Interface Builder中连接它?如果没有,则需要将textfield实例添加到头文件并使其成为合成属性。在头文件中: @interface AppController { NSTextField *myTextField; } @property (nonatomic, retain) IBOutlet NSTextField *myTextField; @接口应用控制器 { NSTextField*myTextField; } @属性(非原子,保留)IBNStextField*myTextField; 在实施过程中: @synthesize myTextField; @合成myTextField; 然后在IB中,控制从文本字段拖动到“文件的所有者”。将出现一个弹出菜单,您应该能够将其连接到“myTextField” 要获取int值,您应该能够执行以下操作:
int RecLine = [textField1.text intValue];
[myTextField.stringValue intValue]
[myTextField.stringValue intValue]
您不必首先获取文本值。NSTextField继承自具有intValue方法的NSControl,因此
-(IBAction)buttonClicked:(id)sender
{
int intVal = [textField intValue];
NSLog (@"Int value is %i", intVal);
}
您是否在代码和IB之间连接了textfield?您需要在@interface声明中定义ivar和属性,如下所示:
@implementation AppController
- (IBAction)StartReconstruction:(id)sender {
int RecLine = //here i need something like textField1.GetIntValue();
drawGL(RecLine);
}
@end
@interface BlahBlah {
UITextField *textField1;
}
@property (nonatomic, retain) IBOutlet UITextField *textField1;
...
@end
在您声明了ivar并将其连接到IB中的文本框(搜索google以了解如何操作)后,您只需拨打
[textField1.text intValue];
要在文本框中获取字符串的整数值(请注意,这是快速且脏的,并且不会验证输入)。要使用属性,您需要将项目的基本SDK更改为OS X 10.5或更高版本。检查此菜单:
Project > Edit Project Settings > Build > Base SDK
NSString*x=self.textfield1.text
Int recLine=[x intValue];“textField1.text”适用于UITextField,但不适用于NSTextField。除非我弄错了,否则我认为这是一个Cocoa问题,而不是Cocoa Touch。您已经学习了Objective-C语法吗?非常确定
*
不应该在那里;NSTextField
,不是他问的。你的代码与前者不兼容。