Bazinga
Bazinga

Reputation: 2466

Cant remove Subview of view

I can't seem to achieve removing a subview of a subview of a view. Its like this:

[self.view addSubview:myView];

[myView addSubview:overlay];

My .h :

@interface

    UIView *myView;
    IBOutlet Overlay *overlay;
...
}

@property (strong, nonatomic) UIView *myView;
@property (strong, nonatomic) IBOutlet Overlay *overlay;
...

.m :

-(void) method {   
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(420.0f,0.0f,604.0f,768.0f)];
    [self.view addSubview:myView]; 

    Overlay *o = [[Overlay alloc] initWithFrame:CGRectMake(000.0f,000.0f,604.0f,768.0f)];
    [myView addSubview:o];
}

I tried [myView removeFromSuperview] and[myView removeFromSuperview]inside the void method, it works. What I needed is to make it work outside or into a different void but what Im doing is not working.

Note:

It says when I alloc myView "Local declaration of 'myView' hides instance variable."

Upvotes: 0

Views: 631

Answers (1)

Evan Mulawski
Evan Mulawski

Reputation: 55334

Replace UIView *myView = (init code) with self.myView = (init code). You are redeclaring myView as a local variable that already has scope within your class, since you made it a property. (The same thing goes for Overlay.)

Then, you can use [self.myView removeFromSuperview] anywhere in this class.

Upvotes: 2

Related Questions