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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:49:34+00:00 2026-06-09T11:49:34+00:00

I am at the 17th chapter of Aaron’s Cocoa programming for Mac os X,

  • 0

I am at the 17th chapter of Aaron’s Cocoa programming for Mac os X, and in the example he embeds a NSView in a NSScrollView.
For exercise I also have added a NSButton programmatically to the view.
The problem is a strange behaviour of the button, which firstly appears on the scroll view, but when I move down the vertical scroller, the button disappears and reappears in the bottom of the scroll view.Since this may be confusing (and also hard to explain), I have made a video to better describe the problem:

http://tinypic.com/player.php?v=k1sacz&s=6

I have subclassed NSView and called the class StretchView (as the book says).
This is the code:

#import <Cocoa/Cocoa.h>

@interface StretchView : NSView
{
@private
    NSBezierPath* path;
}

- (NSPoint) randomPoint;
- (IBAction) click : (id) sender;

@end


#import "StretchView.h"

@implementation StretchView

- (void) awakeFromNib
{
    // Here I add the button
    NSView* view=self;
    NSButton* button=[[NSButton alloc] initWithFrame: NSMakeRect(10, 10, 200, 100)];
    [button setTitle: @"Click me"];
    [button setTarget: self];
    [button setAction: @selector(click:)];
    [view addSubview: button];
}

- (IBAction) click:(id)sender
{
    NSLog(@"Button clicked");
}

- (void) drawRect:(NSRect)dirtyRect
{
    NSRect bounds=[self bounds];
    [[NSColor greenColor] set];
    [NSBezierPath fillRect: bounds];
    [[NSColor whiteColor] set];
    [path fill];
}

- (id) initWithFrame:(NSRect)frameRect
{
    self=[super initWithFrame: frameRect];
     if(self)
    {
        // here i dra some random curves to the view
        NSPoint p1,p2;
        srandom((unsigned int)time(NULL));
        path=[NSBezierPath bezierPath];
        [path setLineWidth: 3.0];
        p1=[self randomPoint];
        [path moveToPoint: p1];
        for(int i=0; i<15; i++)
        {
            p1=[self randomPoint];
            p2=[self randomPoint];
            [path curveToPoint: [path currentPoint] controlPoint1: p1 controlPoint2: p2 ];
            [path moveToPoint: p1];
        }
        [path closePath];
    }
    return self;
}

- (NSPoint) randomPoint
{
    NSPoint result;
    NSRect r=[self bounds];
    result.x=r.origin.x+random()%(int)r.size.width;
    result.y=r.origin.y+random()%(int)r.size.height;
    return result;
}

@end

Questions:

1) Why does the button disappear – reappear and how to avoid this problem?
2) Why are the curves filled in white? I wanted to draw them as tiny lines, not filled.

  • 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-06-09T11:49:36+00:00Added an answer on June 9, 2026 at 11:49 am

    Part 1:

    What it looks like is that the scroll view isn’t updating its scroll bars to where the view is actually scrolled. (Upon launch, it seems like it’s showing that the view is scrolled down to the bottom left already, even when the scroll bar is at the top).

    I can only ask, for now, what OS are you running? It works perfectly for me on Mountain Lion, unless I didn’t duplicate your code correctly.


    Part 2:

    The paths are filled because you used [path fill] in drawRect. Use [path stroke] instead, for strokes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that has a date format of HH:mm so for example
I want to generate a color palette of every 5th, 15th,17th or 51th RGB-value.
** EDIT November 17th ** Hi again, I wanted to show some code you
Given an arbitrary string, for example ( I'm going to play croquet next Friday
I have a table for appointments containing dates and Pupil IDs which I am
Let's say I have stored a bunch of holidays in my database. What I
I have a collection of documents and I'm trying to pull the dates out
my mail have some tempalte with HTML tags includes and images.. when i am
I've used this example on the PHP site to send an HTML email, but
The faulty result I get now is: 17th of July, 2011Today is . function

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.