Alex
Alex

Reputation: 26624

Passing object from controller to a view

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

Upvotes: 2

Views: 2786

Answers (4)

donks
donks

Reputation:

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.

Upvotes: 0

Alex
Alex

Reputation: 26624

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]);
} 

Upvotes: 1

wisequark
wisequark

Reputation: 3268

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

Upvotes: -1

diclophis
diclophis

Reputation: 2442

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

Upvotes: 2

Related Questions