我只是尝试了解如何将值从AppDelegate中的文本字段传递给我的NSView子类“ViewController”。我真的不明白。我是新的Objective-C,我开始感到沮丧。请不要告诉我看书。我有Hillegass COCOA编程,甚至在那里我没有找到我的答案。叫我白痴,而我可以处理,只要我得到的东西...... 我只是试图通过输入变量balkenHoehe设置矩形高度:objective-c与变量的缺失
我的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
我CustomView .H:
#import <Cocoa/Cocoa.h>
@interface CustomView : NSView
{
double balkenHoehe;
}
-(double) balkenHoehe;
-(void) setBalkenHoehe:(double)abalkenHoehe;
@end
我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
你在混淆视图和视图控制器 –
感谢您的回答。我已经了解它并将视图更改为CustomView。使用viewcontroller会帮助我通过用户输入更改drawrect吗? – JFS