JFS
JFS

Reputation: 3152

objective-c drawrect with variables

I simply try to understand how to pass values from a textfield in AppDelegate to my NSView subclass "ViewController". I really don't get it. I'm new in objective-c and I start getting frustrated. Please just don't tell me to read books. I have Hillegass COCOA programming and even there I didn't find my answers. Call me rather idiot I can handle that as long I get that stuff... I simply try to set the rectangle hight by the input variable balkenHoehe:

my AppDelegate .h:

#import <Cocoa/Cocoa.h>  
@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *eingabeText;

- (IBAction)pushButton:(NSButton *)sender;

@end

my AppDelegate .m:

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSLog(@"did launch");
}

- (IBAction)pushButton:(NSButton *)sender {

    NSLog(@"Button pushed");
    double number;

    number = [_eingabeText doubleValue]; //input by textfield

    CustomView *hoehe = [[CustomView alloc] init];
    [hoehe setBalkenHoehe:number];

}
@end

my CustomView .h:

#import <Cocoa/Cocoa.h>

@interface CustomView : NSView
{
   double balkenHoehe;
}

-(double) balkenHoehe;
-(void) setBalkenHoehe:(double)abalkenHoehe;
@end

my CustomView .m:

#import "CustomView.h"

@implementation CustomView

//********************************
-(void) setBalkenHoehe:(double)abalkenHoehe
{
    balkenHoehe = abalkenHoehe;
    [self setNeedsDisplay:YES];
}

//********************************
-(double)balkenHoehe
{
return balkenHoehe;

}

//********************************
- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {

    [self setBalkenHoehe:10]; //initial hight at start


    }
    return self;
}

//********************************
- (void)drawRect:(NSRect)rect
{
    NSRect bounds = [self bounds];
    float fieldWith = bounds.size.width / 3.0;
    float fieldHeight = bounds.size.height / 3.0;


        //background
    NSRect hintergrundRect =
    hintergrundRect = NSMakeRect(bounds.origin.x, bounds.origin.y,
                             bounds.size.width, bounds.size.height);
    [[NSColor grayColor] set];
    [NSBezierPath fillRect:hintergrundRect];

        //Diagram
    NSRect aRect =
    aRect = NSMakeRect (fieldWith, fieldHeight, fieldWith, balkenHoehe);

        // draw rectangle
    [[NSColor whiteColor] set];
    [NSBezierPath fillRect:aRect];

        // draw rect border
    [[NSColor blackColor] set];
    NSBezierPath *aPath = [NSBezierPath bezierPathWithRect:aRect];
    [aPath setLineWidth:1];
    [aPath stroke];

}

@end

Upvotes: 0

Views: 665

Answers (2)

emrys57
emrys57

Reputation: 6806

Horatiu is right, you're not adding the hoehe NSView to the window, there is nowhere to display it. However, the suggestion he makes is for iOS, I think, while this seems to be OS/X. one way to add the view to the window is to write

[self.window setContentView:hoehe];

in pushButton:. However, this only works once, it then wipes out the view of the button!

A more usable way is to add the hoehe view to the existing window's contentView:

ViewController *hoehe = [[ViewController alloc] init];
[hoehe setFrame:CGRectMake(20, 20, 100, 100)]; // or whatever
[self.window.contentView addSubview:hoehe ];
[hoehe setBalkenHoehe:number];

But be aware that each time you press the button, you create a new NSView and plant it on top of all those gone previously.

Upvotes: 1

Horatiu Paraschiv
Horatiu Paraschiv

Reputation: 1780

I think the problem is not in the way you're setting your variable. As far as I see your view controllers' view is not added in the windows' view hierarchy so your draw rect doesn't do anything or better said doesn't have a context to draw stuff.

Add the ViewControllers' view to the window or to your view hierarchy and things should get going. Something like:

[window addSubview:_viewController.view];

This is all I can think about your problem.

Upvotes: 0

Related Questions