我不明白为什么UIView的removeFromSuperview
正在调用didMoveToSuperview
。
我认为仅在将视图添加到视图时才会调用didMoveToSuperview
。任何人都可以解释为什么removeFromSuperview
正在调用didMoveToSuperview
?
以下是我在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
}
这是调用堆栈,显示如何调用didMoveToSuperview
:
#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
答案 0 :(得分:14)
这是正常行为。只要视图的超级视图发生更改,即使它更改为didMoveToSuperview
,也会调用nil
。您可以检查该方法中的superview是否为nil,以确定它是否已添加到超级视图中或从其中删除,如:
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
}
}