2012-11-30 33 views
0

我只是尝试了解如何将值从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 
+0

你在混淆视图和视图控制器 –

+0

感谢您的回答。我已经了解它并将视图更改为CustomView。使用viewcontroller会帮助我通过用户输入更改drawrect吗? – JFS

回答

1

Horatiu是对的,你没有添加hoehe NSView到窗口,无处显示它。不过,我认为他提出的建议是针对iOS,而这似乎是OS/X。将视图添加到窗口的一种方式是编写

[self.window setContentView:hoehe]; 

in 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和植物吗那些以前。

+0

非常感谢这两个答案。我在OS/X中工作并开始理解。但是,在用户界面上有一个customview,它通过一个初始的矩形链接到我的ViewController类。我想用按钮在高度上改变它。有没有办法将“hoehe”或“balkenhoehe”连接到现有视图? – JFS

+0

您的“ViewController”类令人困惑,因为它不是NSViewController,而是NSView。你可以编辑你的问题(它会帮助你,如果你也编辑你的代码)将'ViewController'改为'CustomView'?而且,你的意思是说这个视图在你开始时已经在XIB中定义过了吗? – emrys57

+0

感谢您跟上我!你是对的。我应该将其称为CustomView。是的,视图在XIB中被定义为CustomView类的一个实例。在开始时,视图显示一个初始矩形,高10'[self setBalkenHoehe:10];'。矩形高度需要通过用户输入(变量)和按钮操作进行更改。 – JFS

0

我觉得这个问题是不是在你设置的变量的方式。据我看到你的视图控制器的视图没有添加到Windows的视图层次结构中,所以你的绘制矩形没有做任何事情或更好的说没有上下文来绘制东西。

将ViewControllers的视图添加到窗口或视图层次结构中,并且事情应该开始。例如:

[window addSubview:_viewController.view]; 

这就是我能想到的所有问题。