简体   繁体   中英

How to notify superview of subview removal

I added a custom UIView to a UIViewController and after some code in the view, I want to remove this view from the UIViewController, but I am not sure how to notify the UIViewController of the UIView's removal.

I am using this method to exit from within the UIView

-(void)exit{
    [self removeFromSuperview];
}

Do I need to set a listener? Any help is appreciated


I posted a detailed solution. Thanks Rage, Bill L, and FreeNickname

Use delegation. Add a protocol to your custom View which implements a method to notify the removal of the subview. Make the View controller the delegate while adding the custom view. In your custom class call the delegate method right before [self removeFromSuperview];

Since it is not convenient to write a code as a comment, I'll write it as an answer. This answer illustrates what @Rage suggested in his answer.

First, you create a @protocol for your CustomView and add a delegate for it. You declare that the delegate should conform to this protocol. Then in your ViewController you implement your protocol and set ViewController as a delegate of your CustomView.

Like so:

CustomView.h:

@protocol CustomViewDelegate<NSObject>

//You can also declare it as @optional. In this case your delegate can
//ignore this method. And when you call it, you have to check, whether 
//your delegate implements it or not.
-(void)viewWasRemoved:(UIView *)view

@end

@interface CustomView: UIView

@property (nonatomic, weak) id<CustomViewDelegate> delegate;

@end

CustomView.m:

@implementation CustomView

-(void)exit {
    [self removeFromSuperview];
    //if your method is NOT @optional:
    [self.delegate  viewWasRemoved:self];

    //if it IS @optional:
    if ([self.delegate respondsToSelector:@selector(viewWasRemoved:)]) {
        [self.delegate viewWasRemoved:self];
    }
}

@end

ViewController.m:

#import "CustomView.h"

@interface ViewController()<CustomViewDelegate>

@end

@implementation ViewController

-(void)someMethod {
    self.customView.delegate = self;
}

-(void)viewWasRemoved:(UIView *)view {
    //Whatever
}

@end

Set up a delegate for it, with a method called viewWasRemoved: or something similar. Set your view's delegate to be the ViewController you want to notify, and then in your exit method, call [self.delegate viewWasRemoved:self]; , which will then kick off the viewWasRemoved: method in your ViewController, where you can do any relevant work you need to do once the view is removed.

I'll post a detailed solution in case it helps anyone out, or if anyone can offer any pointers. Thanks Rage, Bill L, and FreeNickname:

The answer is to use delegation

First I imported the superview to the .h of my subview:

#import "ViewController.h"

Then I add a delegate id to the same file:

@property (weak) id delegate;

Then when initializing the custom UIView in the superview, I set the delegate:

 CustomView *view = [[CustomView alloc]initWithFrame:frame];
 view.delegate = self;

I add this method in the superview for a callback:

- (void) viewWasRemoved: (UIView *) view{
    NSLog(@"removed");
}

Then finally call the method in my subView:

-(void)exit{
    [self.delegate viewWasRemoved:self];
    [self removeFromSuperview];
}

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