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