代码之家  ›  专栏  ›  技术社区  ›  Jameson

在obj-c中忽略子类方法?

  •  1
  • Jameson  · 技术社区  · 14 年前

    我对我的obj-c项目中的一个错误有些困惑。我做的事情很简单,不明白我在这里遗漏了什么。我只是在uiimageview的一个子类中创建一个非常简单的方法,然后实例化该类。当我试图使用我实例中的方法时,编译器抱怨它没有实现(尽管它是)

    如有任何帮助,将不胜感激。

    在我的.h文件中:

    @interface CwheelElement : UIImageView { 
    int type;
    int position;
    int row;
    float rotation;
    }
    
    - (CwheelElement *) initWithType:(int) iType andPosition:(int) iPosition onRow:(int) iRow;
    - (void) rotateByRadians:(float) iRadians;
    
    - (void) test;
    
    @property (nonatomic, assign) int type;
    @property (nonatomic, assign) int position;
    @property (nonatomic, assign) int row;
    
    @end
    

    在我的.m文件中:

    - (CwheelElement *) initWithType:(int) iType andPosition:(int) iPosition onRow:(int) iRow {
     self = [super init];
    
     /* …stuff */
    
     return self;
    }
    
    - (void) test {
     NSLog(@"testing");
    }
    

    尝试使用类:

    CwheelElement *iElement = [[CwheelElement alloc] initWithType:row3WheelTypes[i] andPosition:i onRow:3];
       [lowerWheelElements addObject:iElement];
    
       [iElement test];
    

    我得到以下错误:

    2010-09-13 02:13:08.431喷丝板[7329:207] -[uiimageview test]:发送到实例0x4d0e710的无法识别的选择器 2010-09-13 02:13:08.432喷丝头[7329:207] 由于意外异常“nsInvalidArgumentException”而终止应用程序,原因:“* -[uiimageview test]:发送到实例0x4d0e710'的无法识别的选择器**

    它说[cWheeleElement测试]没有实现,但是它是…你知道怎么回事吗?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Jameson    14 年前

    抱歉,我发现在我的初始值设定项中隐藏了以下行:

    self = [[UIImageView alloc] initWithImage:[UIImage imageNamed:pieceGraphicFilename]];
    

    我将其更改为以下内容,不再有问题:

    self.image = [UIImage imageNamed:pieceGraphicFilename];
    
        2
  •  1
  •   Anton Chikin    14 年前

    另外,强烈建议从初始值设定项返回(id),以便在子类化时不会遇到相同的问题。

    - (id) initWithType:(int) iType andPosition:(int) iPosition onRow:(int) iRow;