0

I have a few NSTextFields in my XIB. I created the action for one of my text fields, and it looks like

- (IBAction)setXPos:(id)sender;

In my AppDelegate.h file, I also created an int named XPos. In my AppDelegate.m file, I am having trouble setting the value of XPos to be what I type into the text field. Any help here? Do I need to do a

@property

in my AppDelegate.h? My current code looks like this:

XPos = sender;

But it errors.

XtremeHek3r
  • 381
  • 2
  • 16
  • First, you don't usually set variables in AppDelegate, for what it looks you might wanna set them in your ViewController.. – valbu17 Jan 27 '15 at 03:02
  • find some tutorials... – chris Jan 27 '15 at 03:13
  • @NorthBlast I don't have a viewcontroller. all I have are the app delegate. This is a mac app btw – XtremeHek3r Jan 27 '15 at 03:17
  • @Jason You might have done something wrong because I just created a dummy mac app and it contains/creates the ViewController.. Create new project -> OS X (Application) -> Cocoa Application -> Next -> ProjectName -> Create.. Done! – valbu17 Jan 27 '15 at 03:29
  • What is the IBAction Code? Generally speaking (XPos is a float or a number), you might want to drop a number formatter in your NSTextField, but in any event getting a numeric value is easy enough. Post the IBAction code, and we can help. – jwlaughton Jan 27 '15 at 04:09
  • @NorthBlast is yours document based? Does it matter? – XtremeHek3r Jan 27 '15 at 04:10
  • Can someone just please answer my question? – XtremeHek3r Jan 27 '15 at 04:11

1 Answers1

1

This is quite simple. In your .h file:

@interface AppDelegate : NSObject <NSApplicationDelegate>

{
    IBOutlet NSTextField *xPosTextBox;
}

- (IBAction)setXPos:(id)sender;

Be sure you connect both to your NSTextField in IB. For the next step I'll assume you have a number formatter in the text box and that xPos is a double. In applicationDidFinishLaunching:

[[xPosTextBox formatter] setFormat:@"##0.000"];
[xPosTextBox setDoubleValue:myInitialValue];

Then somewhere in your AppDelegate code add a method:

- (IBAction)setXPos:(id)sender
{
     xPos = [xPosTextBox doubleValue];
}

easy.

if you have a float in applicationDidFinishLaunching:

[[xPosTextBox formatter] setFormat:@"##0.000"];
[xPosTextBox setFloatValue:myInitialValue];

Then the IBAction method:

- (IBAction)setXPos:(id)sender
{
     xPos = [xPosTextBox floatValue];
}

If you have an int (or NSInteger), no need for the number formatter, so in applicationDidFinishLaunching:

[xPosTextBox setIntValue:myInitialValue];
// [xPosTextBox setIntegerValue:myInitialValue]; for NSInteger

Then the IBAction method:

- (IBAction)setXPos:(id)sender
{
     xPos = [xPosTextBox intValue];
    // xPos = [xPosTextBox integerValue]; for NSInteger
}
jwlaughton
  • 905
  • 1
  • 6
  • 11
  • Is it possible to do this with a float or int? – XtremeHek3r Jan 27 '15 at 04:32
  • You are the best! Apple makes the control+drag kinda confusing to a novice programmer like me. – XtremeHek3r Jan 27 '15 at 04:39
  • Also, does this keep checking the value, or does it check only once (in `applicationDidFinishLaunching:` – XtremeHek3r Jan 27 '15 at 04:42
  • Sorry for all the questions, but where did the myInitialValue come from? Is it already defined? (it's late and ill probably implement this in the morning/after school.) – XtremeHek3r Jan 27 '15 at 04:45
  • I've edited my post. In IB if you select this text field then in the Attributes inspector you can choose when the IBAction will be called. I typically choose "Send on end editing", which will call the IBAction either when the user hits enter or tabs away from the field. – jwlaughton Jan 27 '15 at 04:47
  • myInitialValue is just a dummy I put there. I assume you want to set the text box to some initial value when it's first displayed. If you've initialized xPos, you could substitute xPos for myInitialValue. – jwlaughton Jan 27 '15 at 04:50