我正在创建自定义 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
那么我错过了什么?,为什么这不被评估为是??
谢谢!
最佳答案
您已将 NSLog 放在 initWithFrame 方法中。这是在设置 textField.hasBackGroundImage 之前的行上调用的,此时值仍然是 NO。
如果您希望看到设置此值,请将以下方法添加到 MyCustomTextField:
- (void)setHasBackGroundImage:(BOOL)flag
{
_hasBackGroundImage = flag;
}
然后在这个方法上打断点。您应该会在设置后立即看到它。
关于iphone - ios objective C Bool 未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10220217/