views:

443

answers:

2

this is my inherited class of UIView : MobileView

@interface MobileView : UIView {
    IconView *icon1;
    IconView *userCar;
    id goTimer;
}

@property (nonatomic, retain) IconView *icon1;
@property (nonatomic, retain) IconView *userCar;

-(void) goRightInSeconds: (NSInteger)secs;
-(IconView *) cannon;

@end

I also have another class which inheritated UIView: IconView

@interface IconView : UIView {
    UIImage *iconImage;
    CGFloat originalX, originalY;
}

@property (nonatomic, retain) UIImage *iconImage;

-(id)initWithImage: (UIImage *)image andFrame:(CGRect) rect;

-(CGFloat) getX;

-(CGFloat) getY;

@end

Now, in another "main view" class which is also a subclass of UIVIew, I call this:

mobile = [[MobileView alloc] initWithFrame:CGRectMake(0, 100, 150, 150)];
***[mobile cannon]***

Now i have an error: [UIView cannon]: unrecognized selector sent to instance 0xd1ae00' (I still have the same error if I use [mobile icon1])

Why it doesn't regconize the mobile as a MobileView object but an UIView?

+1  A: 

Do you have:

- (id) initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
    // ...
  }
  return self;
}

in your UIView subclass @implementation?

Alex Reynolds
I used this:self = [[UIView alloc] initWithFrame: frame];I guess that caused the problem.YOU ARE THE MAN, ALEX.
SimpleCode
@AlexIs there a need to rewrite constructors for all subclasses?
Denis M
I would override the initWithFrame and dealloc methods for subclassing UIView. Other subclass types may require other methods to be overridden.
Alex Reynolds
+1  A: 

Before you call

mobile = [[MobileView alloc] initWithFrame:CGRectMake(0, 100, 150, 150)];

How do you define mobile? Not as a UIView *, I hope.

This is one way to do it:

MobileView *mobile = [[MobileView alloc] initWithFrame:CGRectMake(0, 100, 150, 150)];
mahboudz
no i defined mobile as a MobileView. The problem was in the init func that I usedself = [[UIView alloc] initWithFrame: frame]but not this(self = [super initWithFrame:frame])
SimpleCode