【问题标题】:ios objective C Bool not settingios 目标 C Bool 未设置
【发布时间】:2012-04-30 12:05:15
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: iphone objective-c ios boolean


    【解决方案1】:

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

    如果你想看到这个值被设置,将以下方法添加到 MyCustomTextField:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2015-05-31
        • 1970-01-01
        • 2019-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-07
        • 1970-01-01
        相关资源
        最近更新 更多