Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ryanknu

macrumors newbie
Original poster
Sep 12, 2008
12
0
This probably seems like a dumb question but I cannot find the answer because I have no idea how to word it.

I want to have a custom NSView subclass connected via an IBOutlet to my controller object. I want clicks received by the subclass to be sent back to the controller object.

Any way to have both objects referring to each other? I've tried using @class directives to make it compile but I still get "object may not implement ... " warnings. Any help would be useful!
 
Simplest approach is to add an ivar in your view subclass "id controller;" and then connect that from your view to the controller in IB. Then in the .m file of your view do "#import <WhateverController.h>", declare the method you wanted called back within WhateverController.h and then call that in the mouseDown: method in your view.

So something like this (untested):
Code:
// Controller.h
@class View;
@interface Controller : NSObject {
    IBOutlet View *view;
}
- (void)handleViewMouseDown:(NSEvent *)event;
@end

// Controller.m
#import "Controller.h

@implementation Controller
- (void)handleViewMouseDown:(NSEvent *)event {
    // do something with the event
}
@end

// View.h
@interface View : NSView {
    id controller;
}
@end

// View.m
#import "View.h"
#import "Controller.h"

@implementation View
- (void)mouseDown:(NSEvent *)event {
    [controller handleViewMouseDown:event];
}
@end
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.