3

我正在创建一个自定义 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

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

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

谢谢!

4

2 回答 2

8

您已将 NSLog 放在 initWithFrame 方法中。这在设置 textField.hasBackGroundImage 之前的行上调用,此时该值仍为 NO。

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

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

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

于 2012-04-19T01:03:58.257 回答
4

您正在调用initWithFrame:,它会打印“nana tienes”,因为 a 的默认值为BOOLFALSENO)。然后,您将其设置为YES after

如果在将其设置为 后进行测试YES,它将打印“lo tienes”。

于 2012-04-19T01:03:21.847 回答