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 AppControlle开发者_如何学Pythonr
- (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
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).
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);
}
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:
@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".
To get the int value, you should be able to do:
[myTextField.stringValue intValue]
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.
To use properties you will need to change your project's base SDK to OS X 10.5 or higher. Check this menu:
Project > Edit Project Settings > Build > Base SDK
精彩评论