我只是想了解如何将值从 AppDelegate 中的文本字段传递到我的 NSView 子类“ViewController”。我真的不明白。我是 Objective-c 的新手,我开始感到沮丧。请不要告诉我看书。我有 Hillegass COCOA 编程,即使在那里我也没有找到答案。叫我白痴,只要我得到这些东西,我就可以处理...我只是尝试通过输入变量balkenHoehe设置矩形高度:
我的 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
我的 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
我的自定义视图 .h:
#import <Cocoa/Cocoa.h>
@interface CustomView : NSView
{
double balkenHoehe;
}
-(double) balkenHoehe;
-(void) setBalkenHoehe:(double)abalkenHoehe;
@end
我的自定义视图 .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