Results 1 to 2 of 2

Thread: Custom View problems...

  1. #1

    Member Since
    Sep 14, 2008
    Custom View problems...
    All right. I'm trying to work with custom views a little and this is confusing me.

    I created the custom view and wrote the files, and this is my app controller calling a method on an object of my custom class:

    - (IBAction)fillBoxes:(id)sender {
    	float topCircleValue = [topCircle floatValue];
    	float bottomCircleValue = [bottomCircle floatValue];
    	NSLog(@"We're in! %f", topCircleValue);
    	[leftFillBox drawFillWithSliderValue:topCircleValue];
    	[rightFillBox drawFillWithSliderValue:bottomCircleValue];
    left and rightFillBox are my objects of class CustomView. Here's my CustomView.h file:

    #import <Cocoa/Cocoa.h>
    @interface CustomView : NSView {
    - (void)drawFillWithSliderValue:(CGFloat)sliderValue;
    And .m file:

    #import "CustomView.h"
    @implementation CustomView
    - (void)drawFillWithSliderValue:(CGFloat)sliderValue {
    	int sliderValueWholeNumber = (int)sliderValue;
    	NSLog(@"%d", sliderValueWholeNumber);
    	NSColor* red = [NSColor redColor];
    	[red set];
    	NSRect selfTest = [self bounds];
            NSLog(@"%@", NSStringFromRect(selfTest));
    All of the NSLog's are behaving exactly as they should. My objective is to get it to draw a rect using the bounds with the height coordinate replaced with sliderValueWholeNumber, but I can't even get this simple test to work. Every time, nothing. It's not filling the custom view area with red. What am I doing wrong?

  2. #2

    Member Since
    Jan 25, 2009
    In your method drawFillWithSliderValue: sliderValue, you must call
    [self setNeedsDisplay:YES]
    The actual drawing takes place in the method -(void)drawRect: (NSRect)rect which is called automatically by Cocoa when the View is displayed on the screen.
    I recommend you to
    1. rename your method setSliderValue; you will be fully Cocoa compliant
    2. keep only the setting of sliderValueWholeNumber in this method and the call to setNeedsDisplay
    3. put the drawing instructions of your method into a drawRect method (superseding the empty NSView method)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Draw text centered in Custom view
    By gonche1124 in forum macOS - Development and Darwin
    Replies: 0
    Last Post: 06-03-2010, 02:23 PM
  2. Problems with custom icons and transparency
    By Martinus in forum macOS - Operating System
    Replies: 0
    Last Post: 09-22-2009, 11:14 AM
  3. Make custom view send action message.
    By Meds in forum macOS - Development and Darwin
    Replies: 0
    Last Post: 01-26-2009, 08:16 PM
  4. One tiny thing missing from this custom view.
    By Meds in forum macOS - Development and Darwin
    Replies: 0
    Last Post: 01-26-2009, 12:43 AM
  5. Cover Flow view problems
    By Conchord in forum iPod Hardware and Accessories
    Replies: 1
    Last Post: 07-03-2007, 02:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts