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 3493516
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:53:31+00:00 2026-05-18T11:53:31+00:00

I’m new to objective-c so bear with me. This seems pretty simple but I

  • 0

I’m new to objective-c so bear with me. This seems pretty simple but I must be doing something wrong. In IB I created an object, called it AppController and then added an action titled makeView and two IBOutlets, btnMakeView and mainWin, then connected everything properly and wrote the class file from that.

In AppController.h I have this code:

#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
    IBOutlet NSWindow * mainWin;
    IBOutlet id btnMakeView;
}
- (IBAction)makeView:(id)sender;
@end

in AppController.m this code:

#import "AppController.h"
#import "ViewController.h"
@implementation AppController
- (IBAction)makeView:(id)sender {

    //declare a new instance of viewcontroller, which inherits fromNSView, were going to allocate it with 
    //a frame and set the bounds of that frame using NSMakeRect and the floats we plug in.
    ViewController* testbox = [[ViewController alloc]initWithFrame:NSMakeRect(10.0, 10.0, 100.0, 20.0)];

    //this tells the window to add our subview testbox to it's contentview
    [[mainWin contentView]addSubview:testbox];
}
-(BOOL)isFlipped {
    return YES;
}
@end

in ViewController.h I have this code:

#import <Cocoa/Cocoa.h>
@interface ViewController : NSView {
}
@end

and finally in ViewController.m I have this code:

#import "ViewController.h"

@implementation ViewController    
- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)drawRect:(NSRect)rect {
    // Drawing code here.

    //sets the background color of the view object
    [[NSColor redColor]set];

    //fills the bounds of the view object with the background color
    NSRectFill([self bounds]);
}

@end

It compiles fine, there are no errors but do not get an object, which I am assuming will be a rectangle, starting at x,y 10/10 and being 100×20. What am I doing wrong?

  • 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-18T11:53:32+00:00Added an answer on May 18, 2026 at 11:53 am

    Does mainWin refer to a valid window? Other than that, your code works, though it could use some improvement. For example,

    • ViewController is a view, not a controller, so it shouldn’t be named a controller.
    • You’ve got a memory leak. You never release the ViewController you create. The memory management rules are very simple; read up on them.

    Try the following changes. First, rename ViewController to RedView (or SolidColorView or somesuch). You can do this very simply by refactoring: right-click the class name in the @interface or @implementation declaration and choose “Refactor…”.

    In RedView.m:

    - (id)initWithFrame:(NSRect)frame {
        // the idiom is to call super's init method and test self all in the
        // same line. Note the extra parentheses around the expression. This is a 
        // hint that we really want the assignment and not an equality comparison.
        if ((self = [super initWithFrame:frame])) {
            // Initialization code here.
        }
        return self;
    }
    

    In AppController.m:

    - (IBAction)makeView:(id)sender {
        //declare a new instance of viewcontroller, which inherits fromNSView, were going to allocate it with 
        //a frame and set the bounds of that frame using NSMakeRect and the floats we plug in.
        RedView* testbox = [[RedView alloc] initWithFrame:NSMakeRect(10.0, 10.0, 100.0, 20.0)];
    
        //this tells the window to add our subview testbox to it's contentview
        // You don't need to keep a reference to the main window. The application already
        // does this. You might forget to hook up mainWin, but the following won't fail
        // as long as the app has a main window.
        [[[[NSApplication sharedApplication] mainWindow] contentView] addSubview:testbox];
        // We're done with testbox, so release it.
        [testbox release];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.