manuelBetancurt
manuelBetancurt

Reputation: 16128

ios objective C Bool not setting

im creating a custom UITextField, but is not setting a 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

and the call in my 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];

so it shows fine, but as you see im setting textField.hasBackGroundImage = YES;

but when executed, i see the message for BOOL = NO??

nana tienes

So what Im I missing?, why is this not evaluated as YES??

thanks!

Upvotes: 3

Views: 1276

Answers (2)

John Graziano
John Graziano

Reputation: 481

You've placed your NSLog in the initWithFrame method. This is called on the line before you set textField.hasBackGroundImage, when the value is still NO.

If you want to see this value get set, add the following method to MyCustomTextField:

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

Then put a breakpoint on this method. You should see it hit as soon as you set it.

Upvotes: 8

Evan Mulawski
Evan Mulawski

Reputation: 55334

You are calling initWithFrame:, which prints "nana tienes" because the default value for a BOOL is FALSE (or NO). Then, you set it to YES after.

If you place the test after you set it to YES, it will print "lo tienes".

Upvotes: 4

Related Questions