このボタン クラスをインスタンス化するときに、次のように追加のパラメータを送信することは可能ですか?
BttClass *myButton = [[BttClass alloc] init];
もしそうなら、正しい構文は何ですか? いろいろ試してみました! BOOL と文字列を送信したいと思います。 ありがとう。
サブクラス化して UIButton を拡張します。
//MyButton.h
@interface MyButton : UIButton {
}
@property (nonatomic, strong) NSString* aString;
@property (nonatomic, weak) BOOL aBool;
初期化メソッドを定義する必要があります
-(id)initWithStringValue:(NSString *)stringValue andWithBoolValue:(bool)boolValue;
次に、初期化メソッドを実装します
//MyButton.m
-(id)initWithStringValue:(NSString *)stringValue andWithBoolValue:(bool)boolValue {
self = [super init];
if (self) {
self.aString = stringValue;
self.aBool = boolValue;
}
return self;
}
呼び出しクラスでは、コードは次のようになります
MyButton* myButton = [[MyButton alloc] initWithStringValue:@"tralala" andWithBoolValue:YES];
カスタム初期化子を定義し、値を渡すだけです。
- (id)initWithBoolValue:(bool)boolValue stringValue:(NSString *)stringValue {
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
カスタム ボタンを作成する
あなたの
CustomButton.h
@interface CustomButton : UIButton
- (id)initWithBooleanValue:(BOOL)value;
@end
そしてあなたの CustomButton.m で
- (id)initWithBooleanValue:(BOOL)value
{
self = [super init];
if (self)
{
NSLog(@"Bool = %d",value);
}
return self;
}
通常の UIButton の代わりにこのクラスを使用できます。