PDA

View Full Version : IB Circular Reference




ryanknu
Feb 18, 2009, 11:55 PM
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!



kainjow
Feb 19, 2009, 05:56 AM
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):
// 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