UIView
基本的にaUILabel
とa のサブクラスである独自のサブクラスを作成UITextField
しています。
のデリゲート メソッドを引き続き機能させたいので、基本的にのデリゲート メソッドを次のようにラップUITextField
する独自のプロトコルを作成しました。MyLabeledInputViewDelegate
UITextField
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [self.delegate inputViewShouldReturn:self];
}
そして、textField は自分のクラスのインスタンスのプロパティであるため、もちろん、次のようにデリゲートを設定します。
if (self.delegate) self.textField.delegate = self;
しかし、MyLabeledInputView
デリゲートを に設定して初期化するとnil
、何らかの理由ですぐにクラッシュするようです。
これを正しく設定していますか、それとも何か不足していますか? どうもありがとうございました!
私の指定された初期化子はこれです:
- (id)initWithFrame:(CGRect)frame
titleRelativeLength:(float)length
titleText:(NSString *)text
titleBackgroundColor:(UIColor *)color
titleTextColor:(UIColor *)textColor
textFieldBGColor:(UIColor *)textFieldBGColor
textFieldTextColor:(UIColor *)textFieldTextColor
delegate:(id<WRLabeledInputViewDelegate>)delegate;
実装はこれです:
- (id)initWithFrame:(CGRect)frame titleRelativeLength:(float)length titleText:(NSString *)text titleBackgroundColor:(UIColor *)color titleTextColor:(UIColor *)textColor textFieldBGColor:(UIColor *)textFieldBGColor textFieldTextColor:(UIColor *)textFieldTextColor
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.titleRelativeLength = length;
self.title = text;
self.titleBackgroundColor = color;
self.titleTextColor = textColor;
self.textFieldBackgroundColor = textFieldBGColor;
self.textFieldTextColor = textFieldTextColor;
}
return self;
}
これは基本的に渡されたプロパティのみをキャプチャし、UITextField のデリゲートをlayoutSubviews
自分のクラスのインスタンスに設定します。