objective-c drawrect with variables



我只是试图理解如何将值从AppDelegate中的文本字段传递到我的NSView子类"ViewController"。我真的不明白。我是objective-c的新手,我开始感到沮丧。请不要叫我看书。我有Hillegass COCOA编程,即使在那里我也没有找到答案。叫我白痴,只要我得到那些东西,我就可以处理......我只是尝试通过输入变量 balkenHoehe 设置矩形高度:

我的应用程序代表 .h:

#import <Cocoa/Cocoa.h>  
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *eingabeText;
- (IBAction)pushButton:(NSButton *)sender;
@end

我的应用程序代表 .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

Horatiu 是对的,您没有将 hoehe NSView 添加到窗口中,没有地方显示它。但是,我认为他提出的建议是针对iOS,而这似乎是OS/X。 将视图添加到窗口的一种方法是编写

[self.window setContentView:hoehe];

pushButton:.但是,这只有效一次,然后它会消除按钮的视图!

一种更有用的方法是将hoehe视图添加到现有窗口的contentView

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

但请注意,每次按下按钮时,您都会创建一个新的 NSView 并将其种植在之前所有 NSView 之上。

我认为问题不在于您设置变量的方式。据我所知,您的视图控制器的视图未添加到窗口的视图层次结构中,因此您的绘图矩形不执行任何操作,或者更好地说没有上下文来绘制内容。

将视图控制器的视图添加到窗口或视图层次结构中,事情应该开始了。像这样:

[window addSubview:_viewController.view];

这就是我能想到的关于你的问题的全部。

相关内容

最新更新