3

私のプロジェクトでは、「美しい」テキスト フィールドを作成するために、角が丸い無地の白い UIButton に埋め込まれた UITextField を使用しています。このコードはさまざまなビューで数回使用されるため、この目的のためにカスタム UIView を作成することにしました。このカスタム UIView のコード:

BSCustomTextField.h

#import <Foundation/Foundation.h>

@interface BSCustomTextField : UIView {
  UIButton* btnTextFieldContainer;
  UITextField* textField;
  NSString* text;
}

@property (retain, nonatomic) UIButton* btnTextFieldContainer;
@property (retain, nonatomic) UITextField* textField;
@property (retain, nonatomic) NSString* text;

-(id)initWithPosition:(CGPoint)position;
@end

BSCustomTextField.m

#import "BSCustomTextField.h"
#import <QuartzCore/QuartzCore.h>

@implementation BSCustomTextField
@synthesize btnTextFieldContainer, textField, text;

-(id)initWithPosition:(CGPoint)position{
   if (self == [super init]) {
      btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)];
      btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
      [[btnTextFieldContainer layer] setCornerRadius:3.];
      [[btnTextFieldContainer layer] setMasksToBounds:YES];
      [[btnTextFieldContainer layer] setBorderWidth:0.];

      textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];

      textField.backgroundColor = [UIColor whiteColor];
      textField.clearButtonMode = UITextFieldViewModeAlways;
      textField.returnKeyType   = UIReturnKeySend;
      textField.keyboardType    = UIKeyboardTypeEmailAddress;
      textField.font               = [UIFont fontWithName:@"Helvetica-Bold" size:20.];

      [btnTextFieldContainer addSubview:textField];
      [self addSubview:btnTextFieldContainer];
   }
   return self;
}

-(void)dealloc{
   [btnTextFieldContainer release];
   [textField release];
   [super dealloc];
}
@end

そのため、コンテナ ビューのviewDidLoadでこのビューを使用すると(以下のコードを参照)、ビューは目的の位置に適切にレンダリングされ、指定されたとおりに見えますが、タッチ イベントには反応しないため、タッチされたときにファーストレスポンダーになりません。

コード:

searchTextField = [[BSCustomTextField alloc] initWithPosition:CGPointMake(30, 150)];
searchTextField.textField.placeholder       = NSLocalizedString(@"lsUserName", @"");
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:searchTextField];

[searchTextField.textField becomeFirstResponder]をプログラムで呼び出すと、正しく機能し、キーボードが表示されます。

しかし、興味深いのは、次のようにBSCustomTextFieldのコードをコンテナーにインラインで埋め込む場合です。

UIButton* btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(30, 150, 260, 50)];
btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
[[btnTextFieldContainer layer] setCornerRadius:3.];
[[btnTextFieldContainer layer] setMasksToBounds:YES];
[[btnTextFieldContainer layer] setBorderWidth:0.];

searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];

searchTextField.backgroundColor = [UIColor whiteColor];
searchTextField.clearButtonMode = UITextFieldViewModeAlways;
searchTextField.returnKeyType   = UIReturnKeySend;
searchTextField.keyboardType    = UIKeyboardTypeEmailAddress;
searchTextField.font            = [UIFont fontWithName:@"Helvetica-Bold" size:20.];
searchTextField.placeholder     = NSLocalizedString(@"lsUserName", @"");

[btnTextFieldContainer addSubview:searchTextField];
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:btnTextFieldContainer];
[btnTextFieldContainer release];

すべてが期待どおりに機能し、テキストフィールドはタッチに反応します。searchTextField のタイプは、各ケースのヘッダー ファイルで適切に設定されます。唯一の違いは、最初のケースでは、UIButton と UITextFiled に追加のラッピング UIView があることです。テキストフィールドをタッチでファーストレスポンダーにするために何をすべきかわかりません。

事前にどうもありがとう、ローマ

4

3 に答える 3

4

わかりました、私は解決策を持っています。UIResponderクラスに対する raaz のポイントは、レスポンダー チェーンに何か問題があるに違いないという考えに私をひっくり返しました。 .
BSCustomTextField.m の新しい作業コードは次のとおりです。

#import "BSCustomTextField.h"
#import <QuartzCore/QuartzCore.h>


@implementation BSCustomTextField
@synthesize btnTextFieldContainer, textField, text;

-(id)initWithPosition:(CGPoint)position{
  if (self == [super init]) {
    btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)];
    btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
    [[btnTextFieldContainer layer] setCornerRadius:3.];
    [[btnTextFieldContainer layer] setMasksToBounds:YES];
    [[btnTextFieldContainer layer] setBorderWidth:0.];

    textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];

    textField.backgroundColor = [UIColor whiteColor];
    textField.clearButtonMode = UITextFieldViewModeAlways;
    textField.returnKeyType   = UIReturnKeySend;
    textField.keyboardType    = UIKeyboardTypeEmailAddress;
    textField.font             = [UIFont fontWithName:@"Helvetica-Bold" size:20.];

    [btnTextFieldContainer addSubview:textField];
    [self addSubview:btnTextFieldContainer];
  }
  return self;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    // UIView will be "transparent" for touch events if we return NO
    return YES;
}

-(void)dealloc{
  [btnTextFieldContainer release];
  [textField release];
  [super dealloc];
}
@end 

クリッカブル領域は BSCustomTextField ビュー全体を埋めるため、pointInside:withEvent で単純に YES を返すことができます:

私を正しい方向に向けてくれてありがとう。

于 2010-12-29T13:40:56.237 に答える
1

UIResponder クラスを参照

このクラスにはインスタンス メソッドbecomeFirstResponder:&があります。isFirstResponder:

editingまた、 textfieldプロパティをに設定することを忘れないでくださいYES

于 2010-12-29T11:05:34.493 に答える
0

ああ、あなたはこのやり方で完全に間違っています。伸縮可能な画像を UIImageView に追加し、UITextField の下に配置する必要があります。

UIImage *backgroundImage = [[UIImage imageNamed:@"fieldBackground.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];

または、UIButton で userInteraction を無効にすることもできます。

于 2010-12-29T11:15:11.897 に答える