0

I simply try to understand how to pass values from a textfield in AppDelegate to my NSView subclass "ViewController". I really don't get it. I'm new in objective-c and I start getting frustrated. Please just don't tell me to read books. I have Hillegass COCOA programming and even there I didn't find my answers. Call me rather idiot I can handle that as long I get that stuff... I simply try to set the rectangle hight by the input variable balkenHoehe:

my 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

my 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

my CustomView .h:

#import <Cocoa/Cocoa.h>

@interface CustomView : NSView
{
   double balkenHoehe;
}

-(double) balkenHoehe;
-(void) setBalkenHoehe:(double)abalkenHoehe;
@end

my 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
JFS
  • 2,992
  • 3
  • 37
  • 48
  • you're mixing up view and viewcontroller – Daij-Djan Nov 30 '12 at 23:10
  • Thanks for your answer. I've learned it already and changed the view to CustomView. Would using viewcontroller help me to change drawrect by user input? – JFS Dec 04 '12 at 08:01

2 Answers2

1

Horatiu is right, you're not adding the hoehe NSView to the window, there is nowhere to display it. However, the suggestion he makes is for iOS, I think, while this seems to be OS/X. one way to add the view to the window is to write

[self.window setContentView:hoehe];

in pushButton:. However, this only works once, it then wipes out the view of the button!

A more usable way is to add the hoehe view to the existing window's contentView:

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

But be aware that each time you press the button, you create a new NSView and plant it on top of all those gone previously.

emrys57
  • 6,679
  • 3
  • 39
  • 49
  • Thanks a lot for both answers. I'm working in OS/X and start to understand. However, there is a customview on user interface linked to my ViewController class with an initial rectangle. I want to change it in the hight by the button. Is there a way to get `hoehe` or `balkenhoehe` connected to the existing view? – JFS Dec 03 '12 at 08:40
  • Your "ViewController" class is confusing, because it is not an NSViewController, it's an NSView. Can you edit your question (and it would help you if you edited your code too) to change `ViewController` to `CustomView`? And, do you mean that this view is already defined in the XIB when you start? – emrys57 Dec 03 '12 at 08:47
  • Thanks for keeping up with me! You are right. I should call it CustomView. And yes, the view is defined in the XIB as an instance of the CustomView class. At start the view shows an initial rectangle with the hight 10 `[self setBalkenHoehe:10];`. The rectangle hight needs to be changed by user input (variable) and button action. – JFS Dec 04 '12 at 07:44
  • You need to find a pointer to the CustomView object in the XIB. In IB, control-drag from the CustomView into the header file where you want the pointer (use the assistant editor) and define an IBOutlet there, like `@property (weak, nonatomic) IBOutlet UIView *myCustomView;` Then you can write `[myCustomView setBalkenHoehe:number];`. I hope! – emrys57 Dec 04 '12 at 08:18
  • AWESOME!!! I got it working finally. GREAT! I had to dance here, honestly! I put `@property (weak, nonatomic) IBOutlet CustomView *myCostumView;` in the .h-file of the class where the button action is described and had just to add `[_myCostumView setBalkenHoehe:number];` in the associated .m-file. A cheer for emrys57 and stack overflow.com. One last question. Is there a good source for greenhorns to learn objective-s for OS/X you would recommend? – JFS Dec 04 '12 at 09:05
  • I used http://www.amazon.co.uk/Cocoa-Programming-Mac-OS-X/dp/0321774086/ref=sr_1_1?ie=UTF8&qid=1354615905&sr=8-1 which is very good. But that doesn't, I think, have the latest xcode 4.5 goodies, especially http://clang.llvm.org/docs/ObjectiveCLiterals.html which are wonderful. Having struggled to learn php from a kindle book, I'd recommend the dead tree version, personally. Buy the book, certainly, but there's nothing to beat getting stuck in to your own projects. Go for it! – emrys57 Dec 04 '12 at 10:17
0

I think the problem is not in the way you're setting your variable. As far as I see your view controllers' view is not added in the windows' view hierarchy so your draw rect doesn't do anything or better said doesn't have a context to draw stuff.

Add the ViewControllers' view to the window or to your view hierarchy and things should get going. Something like:

[window addSubview:_viewController.view];

This is all I can think about your problem.

Horatiu Paraschiv
  • 1,750
  • 2
  • 15
  • 37