ios目标C Bool没有设置

man*_*urt 3 iphone boolean objective-c ios

即时创建自定义UITextField,但是没有设置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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

和我在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];
Run Code Online (Sandbox Code Playgroud)

所以它显示很好,但正如你看到即时设置 textField.hasBackGroundImage = YES;

但是当执行时,我看到BOOL = NO的消息?

nana tienes
Run Code Online (Sandbox Code Playgroud)

那么我错过了什么?,为什么这不被评估为是?

谢谢!

Joh*_*ano 8

您已将NSLog放在initWithFrame方法中.当值设置为NO时,在设置textField.hasBackGroundImage之前调用此行.

如果要查看此值的设置,请将以下方法添加到MyCustomTextField:

- (void)setHasBackGroundImage:(BOOL)flag
{
    _hasBackGroundImage = flag;
}
Run Code Online (Sandbox Code Playgroud)

然后在这个方法上加一个断点.您应该在设置后立即看到它.