0
votes

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?

3
Can you provide screenshot same as my screenshot below? - Loquatious
can you log the superview? `NSLog("%@", self.superview) inside the IBAction - Daij-Djan
It is always a good practice to place NSLog inside your callbacks to check whether it is getting called or not. So as Daij asked. - nzs
where the button with event didPressCloseBtn is placed? inside MultipleSlotsClientView or inside other viewcontroller where you are adding MultipleSlotsClientView? - Vaibhav Saran
Your init method will cause a memory leak as you don't use ARC. - Hermann Klecker

3 Answers

2
votes

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

0
votes

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.

0
votes

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.