0

So I've just started with some mac developing. My first project is to develop a text based calculator which only consist of two components, one NSTextField and one NSTextView. The TextField takes user input and display the answer in the TextView. What I would like is to implement a function where if the TextField is active and the up arrow is pressed the input goes through the command history. I know how to implement the history function but not how to capture the keypress. I've searched and found in Capturing "up" and "down" keys pressed in NSTextField that one way is to implement:

- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector {

The problem is that since I'm quite new to mac development I still don't get how to implement the function?

CiN
  • 1
  • This is a delegate function of textview.You have to create a property of the text field in the class – souvickcse May 17 '14 at 19:30
  • The accepted answer of the other question that you link to shows how to implement that method. What exactly is giving you difficulty? – JWWalker May 18 '14 at 03:07

1 Answers1

0
@interface AppDelgate <NSTextViewDelegate>

@property IBOutlet (nonatomic,strong) NSTextView* myTextView;

@end

@implementation

-(BOOL) applicationDidLaunch(..) {
  myTextView.delegate = self;
}

- (void)keyUp:(NSEvent *)theEvent {
//check theEvent.keyCode (125=up arrow)
}

@end
  • Can't get the keyUp function to be called. My NSTextField is called inputString and it has a "Referencing outlet" as App Delegate. Is this correct? Does this mean I don't have to set "inputString.delegate = self" because xcode complains and says it should be "_input...." but if i change the application does not launch. – CiN May 17 '14 at 20:22
  • 1) correctly 2) No, it has to be `self.inputString.delegate = self` as inputString is a property of AppDelegate. –  May 18 '14 at 10:22