0

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

私の 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
4

2 に答える 2

1

Horatiu の言うとおりです。ウィンドウに NSView を追加しているのではなく、表示する場所がありません。ただし、彼の提案は iOS 用であると思いますが、これは OS/X のようです。ビューをウィンドウに追加する1つの方法は、次のように書くことです

[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 が作成され、以前に削除されたすべてのものの上に植えられることに注意してください。

于 2012-11-30T22:46:11.570 に答える
0

変数の設定方法に問題はないと思います。私が見る限り、ビューコントローラーのビューはウィンドウのビュー階層に追加されていないため、描画四角形は何もしないか、描画するコンテキストがありません。

ViewControllers のビューをウィンドウまたはビュー階層に追加すると、作業が開始されます。何かのようなもの:

[window addSubview:_viewController.view];

あなたの問題について私が考えることができるのはこれだけです。

于 2012-11-30T22:23:01.577 に答える