Stefan Stefan - 4 months ago 23
iOS Question

UIView strange behaviour with BOOL

I've made my custom

UIView
that I'm representing on
UIViewController
. View is presented on screen, but BOOL that I've setted on that instance of UIView to YES is not recognized.

Code:

UIView implementation:

- (id)initWithFrame:(CGRect)frame{

self = [super initWithFrame:frame];
if (self) {
[self createInterface];
}
return self;
}

-(void)createInterface
{
if (self.isSplash == YES) {
self.backgroundColor = [UIColor clearColor];
}
else {
self.backgroundColor = DARK_BLUE;
[self setupTimer];

}
....


ViewController:

self.sponsorView = [[SponsorMechanismView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.logo.frame)+10, SCREEN_WIDTH, 100)];
self.sponsorView.isSplash = YES;

[self.view addSubview:self.sponsorView];


So, I set BOOL to YES, but in this UIView it is always NO.

Answer

As Fabio said, createInterface has already been executed.
What you can do is create your own init function. Something like this :

- (id)initWithFrame:(CGRect)frame isSplash(BOOL)isSplash
{
    self = [super initWithFrame:frame];
    self.isSplash = isSplash;
    if (self) {
        [self createInterface];
    }
    return self;
}  

And the call of the function will be like :

self.sponsorView = [[SponsorMechanismView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.logo.frame)+10, SCREEN_WIDTH, 100) isSplash:YES];