Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView removeFromSuperview is calling didMoveToSuperview

Tags:

ios

swift

uiview

I don't understand why UIView's removeFromSuperview is calling didMoveToSuperview.

I would think that didMoveToSuperview is only called when a view is added to a view. Could anyone explain it why removeFromSuperview is calling didMoveToSuperview?

Here is what I am doing in removeFromSuperview:

public override func removeFromSuperview() {

    clearDelegates()

    chaosPad?.removeFromSuperview()
    brushSliders?.removeFromSuperview()
    moodSlider?.removeFromSuperview()
    brushShapeMenu?.removeFromSuperview()
    moodMenu?.removeFromSuperview()
    gravityMenu?.removeFromSuperview()
    rotationMenu?.removeFromSuperview()

    menuGroups = []
    centerButtons = []
    scrollMenuItems = []
    menu?.removeFromSuperview()
    menu = nil

    super.removeFromSuperview()
}

func clearDelegates() {
    chaosPad?.delegate = nil
    viewController = nil
}

Here is the call stack showing how didMoveToSuperview is called:

#0  0x0000000110f39708 in specialized _fatalErrorMessage(StaticString, StaticString, StaticString, UInt) -> () ()
#1  0x000000010b9cb960 in TDTOilistMenuPainting.configureFrames() -> () at /FastDev/TDTPhotoLib/Oilist/Classes/TDTOilistMenuPainting.swift:161
#2  0x000000010b9cb83e in TDTOilistMenuPainting.didMoveToSuperview() -> () at /FastDev/TDTPhotoLib/Oilist/Classes/TDTOilistMenuPainting.swift:146
#3  0x000000010b9cb872 in @objc TDTOilistMenuPainting.didMoveToSuperview() -> () ()
#4  0x000000010f5d1db5 in __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke ()
#5  0x000000010dc87c60 in -[NSISEngine withBehaviors:performModifications:] ()
#6  0x000000010f5d19b1 in -[UIView(Hierarchy) _postMovedFromSuperview:] ()
#7  0x000000010f5cf610 in __UIViewWasRemovedFromSuperview ()
#8  0x000000010f5cf107 in -[UIView(Hierarchy) removeFromSuperview] ()
#9  0x000000010b9c9aba in TDTOilistMenuPainting.removeFromSuperview() -> () at /FastDev/TDTPhotoLib/Oilist/Classes/TDTOilistMenuPainting.swift:73
#10 0x000000010ba6d269 in TDTPaintingViewController.(navigationController(UINavigationController, animationControllerForOperation : UINavigationControllerOperation, fromViewController : UIViewController, toViewController : UIViewController) -> UIViewControllerAnimatedTransitioning?).(closure #2) at /FastDev/TDTPhotoLib/Oilist/Classes/TDTPaintingViewController.swift:2706
like image 984
Jeshua Lacock Avatar asked Nov 14 '16 01:11

Jeshua Lacock


1 Answers

This is normal behavior. didMoveToSuperview is called whenever a view's superview changes, even if it changes to nil. You can check to see if the superview is nil or not inside that method to determine if it was added to or removed from its superview, like:

override func didMoveToSuperview() {
    if let superview = self.superview {
        // the view was added as a subview to superview
    }
    else {
        // the view was removed from its superview
    }
}
like image 139
TylerP Avatar answered Sep 18 '22 14:09

TylerP