IB Circular Reference

Discussion in 'Mac Programming' started by ryanknu, Feb 18, 2009.

  1. macrumors newbie

    Sep 12, 2008
    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!
  2. Moderator emeritus


    Jun 15, 2000
    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;
    // Controller.m
    #import "Controller.h
    @implementation Controller
    - (void)handleViewMouseDown:(NSEvent *)event {
        // do something with the event
    // View.h
    @interface View : NSView {
        id controller;
    // View.m
    #import "View.h"
    #import "Controller.h"
    @implementation View
    - (void)mouseDown:(NSEvent *)event {
        [controller handleViewMouseDown:event];

Share This Page