2012-04-19 59 views
3

即時通訊創建一個自定義的UITextField,但沒有設置布爾?即時通訊客觀C布爾沒有設置

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]; 

因此它顯示精細, 但正如你看到的IM設置textField.hasBackGroundImage = YES;

但是當執行時,我看到BOOL = NO?的消息。

nana tienes 

那麼,我缺少什麼?,爲什麼這不被評估爲YES?

謝謝!

回答

8

您已將NSLog放入initWithFrame方法中。在設置textField.hasBackGroundImage之前,該值在行上被調用,當值仍然爲NO時。

如果你想看到這個值被置,下面的方法添加到MyCustomTextField:

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

然後把一個斷點此方法。你應該看到它一旦你設置它擊中。

+0

好的答案約翰! – 2012-04-19 01:14:34

4

要調用initWithFrame:,打印「娜娜tienes」因爲對於一個BOOL的默認值是FALSE(或NO)。然後,您在之後將其設置爲YES

如果在將測試設置爲YES之後進行測試,則會打印「lones」。