iphone - ios objective C Bool 未设置

标签 iphone objective-c ios boolean

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

相关文章:

iphone - IOS 应用程序及其存储在设备上的数据的大小限制

iphone - 在后台发送邮件

ios - 使用其标签获取 View 时发出警告

iphone - iOS:在 UIWebView 中从 URL 加载音频时出现问题

iphone - NSUserDefaults,取消选中 iPhone 上的凭据删除

objective-c - iBeacon 的准确度是多少

ios - 移动 View 的背景

ios - CLLocationManager 的 AuthorizationStatus 在 iOS 14 上已弃用

ios - 如何使用 Vertical constraintsWithVisualFormat (SWIFT) 实现 UIScrollView

iphone - 如何知道何时使 `NSTimer` 无效