Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:01:54+00:00 2026-05-27T05:01:54+00:00

Im trying to call an IBAction Method in two classes that is called whenever

  • 0

Im trying to call an IBAction Method in two classes that is called whenever a button that I created in interface builder is clicked. What I really want to happen is for a NSRect to appear whenever the button is clicked but the button and the place where I want the NSRect to appear are in separate views, so the button is in View A and the destination for the rect is in View B. I have tried doing this with NSNotificationCenter but it did not work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:01:55+00:00Added an answer on May 27, 2026 at 5:01 am

    You are missing the C in MVC. Cocoa uses the Model-View-Controller design pattern and you seem to be missing a controller.

    You should create a controller class (possibly a subclass of NSWindowController so that it is responsible for the window) that implements an action method such as -buttonPressed: which is connected to your button. The controller should manage the model (which in this case is whatever the rectangles represent) such that when you press the button, the model is updated. The controller should then make your rectangle view redraw itself.

    Your rectangle view should be set up so that it implements a datasource pattern (see the NSTableView datasource implementation for a good example) so that it knows how many and where to draw the rectangles. If you set your controller as the view’s datasource, your view doesn’t need to know anything about the model.

    Your rectangle view should be set up something like this:

    RectangleView.h:
    @protocol RectangleViewDataSource;

    @interface RectangleView : NSView
    @property (weak) id <RectangleViewDataSource> dataSource;
    @end
    
    //this is the data source protocol that feeds the view
    @protocol RectangleViewDataSource <NSObject>
    - (NSUInteger)numberOfRectsInView:(RectangleView*)view;
    - (NSRect)rectangleView:(RectangleView*)view rectAtIndex:(NSUInteger)anIndex;
    @end
    

    RectangleView.m:

    @implementation RectangleView
    @synthesize dataSource;
    
    - (void)drawRect:(NSRect)rect
    {
        //only draw if we have a data source
        if([dataSource conformsToProtocol:@protocol(RectangleViewDataSource)])
        {
            //get the number of rects from the controller
            NSUInteger numRects = [dataSource numberOfRectsInView:self];
            for(NSUInteger i = 0; i < numRects; i++)
            {
                NSRect currentRect = [dataSource rectangleView:self rectAtIndex:i];
                //draw the rect
                NSFrameRect(currentRect);
            }
        }
    }
    @end
    

    You would assign your controller as the datasource of the view and make it implement the RectangleViewDataSource protocol methods.

    The controller would look something like this:

    YourController.h:

    #import "RectangleView.h"
    
    @interface YourController : NSWindowController <RectangleViewDataSource>
    @property (strong) NSMutableArray* rects;
    @property (strong) IBOutlet RectangleView *rectView;
    
    - (IBAction)buttonPressed:(id)sender;
    
    @end
    

    YourController.m:

    @implementation YourController
    @synthesize rects;
    @synthesize rectView;
    
    - (id)init 
    {
        self = [super init];
        if (self) 
        {
            rects = [[NSMutableArray alloc] init];
        }
        return self;
    }
    
    
    - (void)awakeFromNib
    {
        //assign this controller as the view's data source
        self.rectView.dataSource = self;
    }
    
    - (IBAction)buttonPressed:(id)sender
    {
        NSRect rect = NSMakeRect(0,0,100,100); //create rect here
        [rects addObject:[NSValue valueWithRect:rect]];
        [self.rectView setNeedsDisplay:YES];
    }
    
    //RectangleViewDataSource methods
    - (NSUInteger)numberOfRectsInView:(RectangleView*)view
    {
        return [rects count];
    }
    
    - (NSRect)rectangleView:(RectangleView*)view rectAtIndex:(NSUInteger)anIndex
    {
        return [[rects objectAtIndex:anIndex] rectValue];
    }
    
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to call a subview after a button is clicked on my
I am trying to call a shell script that sets a bunch of environment
Hi I am trying to call a method from a method, I am getting
i'm trying to show MBProgressHUD when a button is clicked and at the end
Im trying to call the function: friend ostream& operator<<(ostream& out, stack::myItem& theItem); that is
Im trying to call functions with same signature. Example: There are two functions with
I'm trying to call a Cocoa IBAction from Carbon code... I've set up global
I am trying to call and alert when a button is pressed. i use
When trying to call Close or Dispose on an SqlDataReader i get a timeout
I trying to call 2 functions in the same page to submit jquery-ajax at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.