-1
我想我已經遵循了示例代碼,但是下面的代碼給了我一個錯誤。iOS中的UIButton的簡單子類化
我想子類UIButton並添加了一些屬性,但我從失敗中失敗。
我已經創建了一個子類文件。這些都是我的.h/.M的:
// damButton.h
#import <UIKit/UIKit.h>
@interface damButton : UIButton
{
CGFloat _position;
}
@property (nonatomic) CGFloat position;
@end
和
// damButton.m
#import "damButton.h"
@implementation damButton
@synthesize position = _position;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
@end
在我mainviewcontroller
,我已經導入我的自定義按鈕,但是當我使用屬性的內置getter和setter,我得到一個錯誤:
//MainViewController.m
#import "damButton.h"
// then within a method...
damButton *b = [damButton buttonWithType:UIButtonTypeRoundedRect];
[b position:5.0];
生成此錯誤:No visible @interface for 'damButton' declares the selector 'position:'
我不知道我是什麼在這裏錯過了,我幾乎完全複製它(我認爲)。我只想使用內置的getter/setters(現在)。
我錯過了什麼?
banging head against desk>它一定遲到了,我不敢相信我犯了這樣一個愚蠢的錯誤。我應該刪除這個問題嗎?爲了回答你的問題(簡單地做這件事並不容易),我用十幾個或更多動態創建的按鈕創建一個小型學習遊戲。我使用標籤和標題屬性來跟蹤它,但我需要更多的跟蹤。添加一些屬性將爲我做。我正在使用另一個NSMutableArray,我必須隨按鈕一起更新,我認爲這可能會更容易一些。 – Madivad
@Madivad問題和答案可能對某人有用,所以不要刪除它。我認爲你的用例可以繼承UIButton。但是,請注意'+ buttonWithType:':'此方法是一個便捷的構造函數,用於創建具有特定配置的按鈕對象。它是你的子類UIButton,這個方法不會返回你的子類的一個實例。如果要創建特定子類的實例,則必須直接從[docs](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/)中分配/初始化按鈕。 UIButton/UIButton.html) – hwaxxer
好的,謝謝你。我對所有這些都是陌生的,而且當我把它付諸實踐時,子類化(雖然聽起來很簡單)似乎更難了:) – Madivad