0
votes

lets say I have an NSWindow Class, that has several events for mouse and trackpad movements for example this code

IMHO, I think this should be good programming, it is similar to pointing an action of a button to its method in controller. in MyWindow.m Class I have (which in IB I have set the window class to it)


@implementation MyWindow
- (void)swipeWithEvent:(NSEvent *)event {
NSLog(@"Track Pad Swipe Triggered");
/* I want to add something like this
   Note that, Appdelegate should be existing delegate, 
not a new instance, since I have variables that should be preserved*/
[AppDelegate setLabel];
}   
@end

and in My AppDelegate.h I have

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet NSTextField *label;
}
-(void)setLabel;
@end

and in my AppDelegate.m I have

-(void)setLabel
{
[label setStringValue:@"swipe is triggered"];
}

I have tried #import @class, [[... alloc] init], delegate referencing in IB (I made an object class of MyWindow - thanks to the answer of my previous question ) that latter seems the closest one, it works if both of the classes are delegates, so I could successfully call the "setLabel" action from a button in a second controller (delegate class)'s IBAction method, but this View Events seem not communicating with the delegate's action although their code is executing.

1

1 Answers

1
votes

You are sending a message to the AppDelegate class, not the instance of your AppDelegate class which is accessible from the NSApplication singleton ([NSApplication sharedApplication])

[AppDelegate setLabel];

This is wrong, to get the delegate do this:

AppDelegate* appDelegate = (AppDelegate*)[[NSApplication sharedApplication] delegate];

then send the message to that instance:

[appDelegate setLabel];