我是一個有經驗的C++程序員試圖創建的UILabel的我的第一個Objective-C的子類有一個附加的只讀屬性的UILabel子類出現的UILabel在Objective-C
// UINumericlabel.h
@interface UINumericLabel : UILabel
// Returns true if the numeric display contains a decimal point
@property (readonly,nonatomic) BOOL hasDecimalPoint;
@end
// UINumericLabel.m
#import "UINumericLabel.h"
@implementation UINumericLabel
// Returns true if the numeric display contains a decimal point
- (BOOL) hasDecimalPoint;
{
return [self.text rangeOfString:(@".")].location != NSNotFound;
}
@end
當我嘗試引用hasDecimalPoint屬性的實體化UINumericLabel我得到錯誤中止 2012-02-20 18:25:56.289 Calculator[10380:207] -[UILabel hasDecimalPoint]: unrecognized
選擇發送到實例0x684c5c0
在調試它將我的UINumericLabel屬性聲明顯示爲UILabel * 我需要在我的UINumericLabel子類中重寫UILabel的(id)init嗎?我怎麼做?
#import "UINumericLabel.h"
@interface CalculatorViewController : UIViewController <ADBannerViewDelegate>
@property (weak, nonatomic) IBOutlet UINumericLabel *display0;
@end
當我將鼠標懸停在display0P在調試器它說,這是一個UILabel *不是UINumericLabel *
UINumericLabel * display0P = self.display0;
僅僅聲明該屬性爲「UINumericLabel」類型是不夠的。您需要告訴Interface Builder使用您的子類。 – ipmcc 2012-02-21 02:48:57
ipmcc - 你是否在我的答案中增加了更多信息或者說它不正確。只是想澄清一下。 – Brian 2012-02-21 03:34:49
@ Brian的回覆看起來對我來說是正確的。 – danh 2012-02-21 04:12:31