Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get int from textField

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:

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

thanks for help

like image 801
Tomas Svoboda Avatar asked Feb 01 '11 22:02

Tomas Svoboda


2 Answers

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:

@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

[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).

like image 171
futureelite7 Avatar answered Sep 22 '22 16:09

futureelite7


you don't have to get text value first. NSTextField inherits from NSControl which has intValue method, so...

-(IBAction)buttonClicked:(id)sender
{
    int intVal = [textField intValue];
    NSLog (@"Int value is %i", intVal);
}
like image 41
mcFactor Avatar answered Sep 25 '22 16:09

mcFactor