views:

1842

answers:

6

I'm following iPhone dev courses from Stanford Open-University, and I've been blocked for 2 days on assignment3, maybe someone can help me here?

The tasks are:

  1. Create a custom UIView subclass that will display your PolygonShape object
  2. Give your view class access to the PolygonShape object so that it can retrieve the details of the polygon as needed

The problem is: how do I give my view class access to the polygon object defined in my controller?


Here is my implementations if it can help:

CustomView.h:

#import "PolygonShape.h"

@interface CustomView : UIView {
    IBOutlet PolygonShape *polygon;
}
- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;

@end

Controller.h:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PolygonShape.h"
#import "PolygonView.h"

@interface Controller : NSObject {
    IBOutlet UIButton *decreaseButton;
    IBOutlet UIButton *increaseButton;
    IBOutlet UILabel *numberOfSidesLabel;
    IBOutlet PolygonShape *polygon;
    IBOutlet PolygonView *polygonView;
}
- (IBAction)decrease;
- (IBAction)increase;
- (void)awakeFromNib;
- (void)updateInterface;
@end
+1  A: 

Found my own answer, I missed a setPolygon method in my CustomView to link both... stupid...

in CustomView.h:

#import "PolygonShape.h"

@interface CustomView : UIView {
    IBOutlet PolygonShape *polygon;
}

@property (readwrite, assign) PolygonShape *polygon;

- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;

@end

in CustomView.m:

@implementation CustomView

@synthesize polygon;

...

@end

in Controller.m:

- (void)awakeFromNib { 
    // configure your polygon here 
    polygon = [[PolygonShape alloc] initWithNumberOfSides:numberOfSidesLabel.text.integerValue minimumNumberOfSides:3 maximumNumberOfSides:12];
    [polygonView setPolygon:polygon];
    NSLog (@"My polygon:  %@", [polygon description]);
}
Alex
+2  A: 

And after you figure it out, it might not hurt to touch up on some objective-c basics:

http://www.cocoacast.com/?q=node/103

diclophis
thanks a lot for the link
Alex
A: 

So why aren't you declaring them as properties of the class?

wisequark
A: 

I'm still having trouble with this myself: the polygonView hasn't been instantianted yet in awakeFromNib.

What am I missing? The IB stuff for Controller.xib looks OK, but...

Sam Dutton
A: 

Thanks for the code.

Do you have a PolygonView as well as a CustomView?

My problem is that the CustomView hasn't been instantiated yet when awakeFromNib is called in Controller (and nor is polygon, if you add it via Interface Builder), so this doesn't work:

[polygonView setPolygon:polygon];

The HelloPoly assignment works fine for me: the PolygonShape instance is instantiated before awakeFromNib is called.

I guess my real question is -- how do you make sure objects in the IB document are instantiated before awakeFromNib is called in the File's Owner (in this case, Controller)?

Hmm...

Sam Dutton
A: 

I just finished assignement 3 last night. I solved this connection all in Interface Builder. First I created an outlet on the "PolygonView" UIView subclass for the PolygonShape and then connected it to the instance of the Polygon model. From what I have read in the Google Group and on various other sites, I do not think there is one right way to connect this UIView to the model and the controller. But it worked I think there is nothing wrong with the View knowing about the model.