简体   繁体   中英

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?

Just try to connect like below screenshot, don't connect to FileOwner. 在此输入图像描述

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM