gpt4 book ai didi

ios - 在 for 循环中将 subview 设置为 nil

转载 作者:行者123 更新时间:2023-11-28 09:45:37 25 4
gpt4 key购买 nike

我正在尝试从其父 View 中删除 subview 和 View ,并将它们设置为零。我注意到 removeFromSuperview 方法没有将它们设置为 nil,这不是我想要的。我得到的错误是在 loadingViewSubviews[i] = nil 行。它说:不能通过下标赋值,loadingViewSubviews 是不可变的。我该如何克服这个问题?

这是我在 viewDidDisappear

中使用的代码
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
var loadingViewSubviews = loadingView.subviews
if loadingView != nil{
for var i = 0; i < loadingViewSubviews.count; ++i{
loadingViewSubviews[i].removeFromSuperview()
loadingViewSubviews[i] = nil
}
loadingView.removeFromSuperview()
loadingView = nil
}
}

编辑:此代码创建 loadingView 及其 subview

var activityIndicator: UIActivityIndicatorView!
var activityLabel:UILabel!
var loadingView: UIView!




func processingIAP() {
if loadingView == nil{
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
activityIndicator.hidesWhenStopped = true
let labelRect = CGRectMake(activityIndicator.frame.width+6, 0, 200, activityIndicator.frame.height+12)
activityLabel = UILabel(frame: labelRect)
activityLabel.text = "Accessing store..."
activityLabel.textColor = UIColor.whiteColor()
activityLabel.adjustsFontSizeToFitWidth = true
activityLabel.textAlignment = NSTextAlignment.Center
let rect = CGRectMake(sceneView.frame.width/2.0-(activityIndicator.frame.width/2.0 + activityLabel.frame.width/2.0+12), sceneView.frame.height/2.0-(activityIndicator.frame.height/2.0), activityIndicator.frame.width + activityLabel.frame.width+24, activityIndicator.frame.height+12)
loadingView = UIView(frame: rect)
loadingView.backgroundColor = UIColor.blackColor()
self.sceneView.addSubview(loadingView)
loadingView.addSubview(activityIndicator)
loadingView.addSubview(activityLabel)
loadingView.layer.cornerRadius = 3
loadingView.layer.borderWidth = 3
loadingView.layer.borderColor = UIColor.whiteColor().CGColor
activityIndicator.frame.origin = CGPointMake(6, (loadingView.frame.height-activityIndicator.frame.height)/2.0)
activityIndicator.startAnimating()
}else{
activityLabel.text = "Accessing store..."
activityIndicator.startAnimating()
activityLabel.frame.origin.x += activityIndicator.frame.width
activityLabel.frame.size.width -= activityIndicator.frame.width
}

}

最佳答案

如果您有对您的 subview 的引用,即使从父 View 中移除,它也不会自行销毁。

你可以这样做,但是在 objective-c 中:

while (self.subviews.count > 0) {
id view = [self.subviews lastObject];
[view removeFromSuperview];
view = nil;
}

关于ios - 在 for 循环中将 subview 设置为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32903134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com