views:

31

answers:

1

Coding against UIKit for iPhone.

Setup, with relevant detail:

SomeView.h:

#import <UIKit/UIKit.h>

@interface SomeView : UIView {
    SomeObject *myObject;
}

@property (assign) SomeObject *myObject;

-(void) doSomething;

@end

SomeView.m:

#import "SomeView.h"

@implementation SomeView

@synthesize myObject;

- (void)doSomething {
    NSLog(@"doing something");
}

- (void) drawRect:(CGRect)rect {
    // drawing is based on myObject
}

@end

Controller.h:

#import <UIKit/UIKit.h>
#import "SomeView.h"

@interface Controller : NSObject {
    IBOutlet UIView *someView;
}

@end

Controller.m:

#import "Controller.h"

@implementation Controller

-(void)awakeFromNib {

    [someView doSomething];
    [someView setSomeObject:someObject]; 
}
@end

I am instantiating the controller object in Interface Builder, and SomeView is the class of one of my custom UIViews in my app's main window.

Now, the questions:

1) when I run the above, I get warnings for both lines: "Warning: 'UIView may not respond to 'doSomething'" and similar warning for setSomeObject. Why? (The code actually seems to work, but I don't like seeing the warnings.)

2) is this the right way of doing things? What I am really after, is making SomeView aware of someObject, so that when drawRect for SomeView is called, it can change its behavior based on current state of someObject. I don't need to have the object directly in SomeView; I could have it in the controller, but the view still needs some information from it that may change at runtime.

+3  A: 

You declared someView as an instance of UIView, but doSomething is a method of SomeView. So it is correct — the class you told the compiler that the variable points to does not respond to that message. If you don't want warnings, you'll have to make it a SomeView * instead of a UIView *.

Otherwise, your general architecture looks OK to me.

Chuck
Thanks, that was it, I had the type wrong in controller header.
Jaanus