2012-04-19 7 views
3

カスタムUITextFieldを作成していますが、BOOLを設定していませんか?ios objective C Boolが設定されていません

MyCustomTextField.h

#import <UIKit/UIKit.h> 

@interface OMTextField : UIControl <UITextFieldDelegate> 

{ 
    UITextField *_theTextField; 

    BOOL _hasBackGroundImage; 
} 

@property (nonatomic, retain)UITextField *theTextField; 
@property (nonatomic) BOOL hasBackGroundImage; 

@end 

MyCustomTextField.m

#import "OMTextField.h" 

@implementation OMTextField 

@synthesize theTextField = _theTextField; 
@synthesize hasBackGroundImage = _hasBackGroundImage; 

- (void)dealloc 
{ 
    [_theTextField release]; 
    [super dealloc]; 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 

     self.theTextField = [[[UITextField alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]autorelease]; 
     //self.theTextField.borderStyle = UITextBorderStyleLine; 
     self.theTextField.text = @"textField"; 
     self.theTextField.delegate = self; 

     if (self.hasBackGroundImage) { 
      NSLog(@"lo tienes"); 
     } 
    if (!self.hasBackGroundImage) { 
     NSLog(@"nana tienes"); 
    } 

     [self addSubview:self.theTextField]; 


    } 
    return self; 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 

と私のMainVCでコール:

MyCustomTextField *textField = [[[MyCustomTextField alloc]initWithFrame:CGRectMake(400, 160, 150, 40)]autorelease]; 

    textField.hasBackGroundImage = YES; 

    textField.backgroundColor = [UIColor grayColor]; 
    textField.theTextField.borderStyle = UITextBorderStyleLine; 


    [self.view addSubview:textField]; 

ので、それは罰金を示し、 いますが、イム設定を参照してくださいとしてtextField.hasBackGroundImage = YES;

実行時に、BOOL = NOのメッセージが表示されますか?

nana tienes 

私は何が不足していますか?なぜ、これは「はい」と評価されませんか?

ありがとうございました!

答えて

8

NSLogをinitWithFrameメソッドに配置しました。これは、値がまだNOのときにtextField.hasBackGroundImageを設定する前に、行で呼び出されます。

あなたは、この値が設定されます確認したい場合は、MyCustomTextFieldに次のメソッドを追加します。

- (void)setHasBackGroundImage:(BOOL)flag 
{ 
    _hasBackGroundImage = flag; 
} 

次に、このメソッドにブレークポイントを置きます。あなたはそれを設定するとすぐにヒットするはずです。

+0

敬具John! –

4

あなたはBOOLのデフォルト値はFALSE(またはNO)であるため、「ナナtienes」を印刷しinitWithFrame:を呼んでいます。次に、 に設定し、の後に設定します。

YESに設定した後にテストを配置すると、 "lo tienes"と表示されます。

関連する問題