일부 할당 해제 문제 및 아마도 알아 내지 못하는 강력한 또는 순환 참조를 처리하고 있습니다. 나는 3 개 UIView
들 아래와 같이 인스턴스화 한 :강력한 참조 및 UIView 메모리 문제
나는 스토리 보드에서 내부 UIView
을 추가 한 하나의 주 ViewController
가와 UIView
같은 클래스 안에 weak
콘센트가 있습니다
class ViewController : UIViewController {
//MARK: - outlets
@IBOutlet weak var firstView: FirstUiview!
}
두 번째 UIView
인을 첫 번째 관점에 하위 뷰로서 첨가 프로그래밍 같은
class FirstUiview : UIView {
//creating an instance of secondUiView
lazy var mySecondView: SecondViewClass = {
let dv = SecondViewClass()
dv.backgroundColor = UIColor.red
return dv
}()
//sometime later by clicking on a button
self.addSubview(mySecondView)
//a button will be tapped to remove mySecondView;
//later will be called at some point upon tapping:
func removingSecondViewByTapping() {
if mySecondView.isDescendant(of: self) {
mySecondView.removeFromSuperview()
}
}
}
이제 SecondViewClass
은 다음과 같습니다
mySecondView
를 제거한 다음 내가
mySecondView
의 모든 서브 뷰가 해제 사라 것으로 예상 (같은
ViewController
여전히) 다른 시간에 다시 추가 있지만, 버튼을 탭하면 이제
class SecondViewClass : UIView {
//in this class I create bunch of uiview objects like below:
lazy var aView : UIView = {
let hl = UIView()
hl.tag = 0
hl.backgroundColor = UIColor.lightGray
return hl
}()
self.addSubview(aView) //... this goes on and I add other similar views the same way.
//creating an instance of thirdView
var let thirdView = UIView()
self.addSubview(thirdView)
}
모두 거기에있다. 나는 누군가가 나에게 그것을 지적 할 수 있다면 나는 많은 것을 감사 할 것이다. 나는 강한 참고를 유지하고 있는가? 아니면 순환 참조 문제가 있는가? 아니면 다른 뭔가?
왜 당신이 2 뷰의 하위 뷰를 기대 : 사용자 정의 속성은 뷰 계층 구조에서 제거
weak
을이기 때문에, 당신의weak
참조가 자동으로nil
이 될 것이다 (따라서 유일한 강한 참조를 제거) 없어 졌니? Superview에서'mySecondView'를 제거한다고해서 모든 서브 뷰가 제거되지는 않습니다. – dan@dan 오 잠깐, 나는 view.removeFromSuperview가 공개하고 그 서브 뷰를 포함하여 뷰를 파괴 할 것이라고 생각했는데, 서브 뷰가 보유한 것에 대한 참조가 없다면 그렇지 않습니까? 그렇지 않다면 하위 뷰를 포함하여 보내는보기를 완전히 제거 할 수있는 방법을 안내 할 수 없습니까? – TheeBen