Releasing a UIViewController when its view is removed using removeFromSuperview

I have a basic view that manually creates a UIViewController (and not a UINavigationController) and adds this kind of controller as a slave.

The subview then drops itself through [self removeFromSuperview]. I understand that this frees up the view, so it’s good, but now I also want to get rid of the UIViewController, which I distributed right away when the view was deleted, to free memory and not wait until the main view controller was deleted.

I could have avoided using removeFromSuperview and have a back link to the main view controller and make it refuse to spy and release the controller, but there seems to be a cleaner way.

Is there a best practice for this?

+3
source share
1 answer

The correct way for your subcontroller to ask the main controller to remove it. If you want to reduce the connection between the two controllers, create a delegate protocol for your subcontroller:

// This forward declaration avoids having a circular dependency 
// between MySubcontroller and MySubcontrollerDelegate
@class MySubcontroller;

@protocol MySubcontrollerDelegate

- (void)hideMySubcontroller:(MySubcontroller*)subcontroller;

@end

, , . , , .

delegate :

@interface MySubcontroller : UIViewController {
    id <MySubcontrollerDelegate> delegate;
    ...
}
@property (assign) id <MySubcontrollerDelegate> delegate;
...
@end

removeFromSuperview hideMySubcontroller: , self .

, :

@interface MyMainController : UIViewController <MySubcontrollerDelegate>
...
@end

, subcontroller delegate self. hideMySubcontroller:, , , .

, , ; , - . , .

, , , ; , . ( , , .)

+5

Source: https://habr.com/ru/post/1707459/


All Articles