YosiFZ
YosiFZ

Reputation: 7900

Remove Custom View from superView

I have My own UIView :

#import <UIKit/UIKit.h>

@interface MultipleSlotsClientView : UIView


-(IBAction)didPressCloseBtn:(id)sender;

@end

And this is the implementation:

@implementation MultipleSlotsClientView

- (id)initWithFrame:(CGRect)frame {
    self = [[[[NSBundle mainBundle] loadNibNamed:@"MultipleSlotsClientView" owner:self options:nil] objectAtIndex:0] retain];
    if (self) {
        self.frame = frame;
    }
    return self;
}

#pragma mark
#pragma mark IBAction

-(IBAction)didPressCloseBtn:(id)sender {
    [self removeFromSuperview];
}

@end

And i have a btn that connect to the didPressCloseBtn method and when i press the button the method called but the View won't remove from the superview.

This is how i alloc the UIView and add it:

MultipleSlotsClientView *multiView = [[[MultipleSlotsClientView alloc] initWithFrame:self.view.frame] autorelease];
[self.view addSubview:multiView];

Any idea why the view won't disappear?

Upvotes: 0

Views: 236

Answers (3)

Hermann Klecker
Hermann Klecker

Reputation: 14068

This is an answer to a comment. Just because you cannot format comments nicely. It is about why you leak memory in your code and how you could write the code to overcome the issue.

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Do something here if you have to.
        // Currently there is no reason for overwriting intiWithFrame: at all. 

    }
    return self;
}

And instead of:

MultipleSlotsClientView *multiView = [[[MultipleSlotsClientView alloc] initWithFrame:self.view.frame] autorelease];

Just do:

MultipleSlotsClientView *multiView= [[[[NSBundle mainBundle] loadNibNamed:@"MultipleSlotsClientView" owner:self options:nil] objectAtIndex:0] autorelease];
multiView.frame = self.view.frame;

Well, whether you should retain or autorelease it or nothing of that kind at all depends on the other code. Assuming that you add multiView to the subview hierarchy, which will retain it, autorelease is fine.

Upvotes: 0

LittleIDev
LittleIDev

Reputation: 921

Step-1 write the below method in NSObject Category class

+ (id)loadNibNamed:(NSString *)NibName {
    NSObject *cl = nil;
    if (NSClassFromString(NibName) != nil) {
        NSArray *arr = [[NSBundle mainBundle] loadNibNamed:NibName owner:self options:nil];
        for (id cls in arr) {
            if([cls isKindOfClass:NSClassFromString(NibName)])
            {
                cl = cls;
                break;
            }
        }
    }
    return cl;
}

Step:2 call the respective Class like

MultipleSlotsClientView *multiView = [MultipleSlotsClientView loadNibNamed:@"MultipleSlotsClientView"]
[self.view addSubview:multiView];

And No need to write anything in "initWithFrame". Try this. It may work for you.

Upvotes: 0

Loquatious
Loquatious

Reputation: 1775

Just try to connect like below screenshot, don't connect to FileOwner.enter image description here

Upvotes: 2

Related Questions