【问题标题】:removeFromSuperview() doesn't workremoveFromSuperview() 不起作用
【发布时间】:2018-01-21 11:30:43
【问题描述】:

我有以下类来添加和删除活动指示器视图:

import Foundation
import UIKit

class ViewControllerUtils {

var container: UIView = UIView()
var loadingView: UIView = UIView()
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

func showActivityIndicator(uiView: UIView) {
    container.frame = uiView.frame
    container.center = uiView.center
    container.backgroundColor = UIColorFromHex(rgbValue: 0xffffff, alpha: 0.3)

    loadingView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
    loadingView.center = uiView.center
    loadingView.backgroundColor = UIColorFromHex(rgbValue: 0x444444, alpha: 0.7)
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 10

    activityIndicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40);
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
    activityIndicator.center = CGPoint(x: loadingView.frame.size.width / 2, y: loadingView.frame.size.height / 2);

    loadingView.addSubview(activityIndicator)
    container.addSubview(loadingView)
    uiView.addSubview(container)
    activityIndicator.startAnimating()
}

func hideActivityIndicator(uiView: UIView) {
    print("hideActivityIndicator called")

    activityIndicator.stopAnimating()
    activityIndicator.removeFromSuperview()
    loadingView.removeFromSuperview()
    container.removeFromSuperview()
}

func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor {
    let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
    let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
    let blue = CGFloat(rgbValue & 0xFF)/256.0
    return UIColor(red:red, green:green, blue:blue, alpha:CGFloat(alpha))
}

}

我在 tableview 和 view 中调用函数,方法如下:

override func viewDidLoad() {
    super.viewDidLoad()
    ViewControllerUtils().showActivityIndicator(uiView: self.tableView)

    invitedEvents()
}

加载数据后,我尝试删除活动指示器,但没有删除。

func invitedEvents() {
     //calling firebase
     DataService.ds.REF_USER_CURRENT.observe(.value, with: { (snap) in
        ViewControllerUtils().hideActivityIndicator(uiView: self.tableView)
     })
}

当 tableview 正在加载时,正在打印 hideActivityIndi​​cator 被调用,但它没有从视图中删除。

我也在视图中调用函数,结果相同:showActivityIndi​​cator 工作,hideActivityIndi​​cator 不工作。

我使用以下参数在视图中调用函数:

  ViewControllerUtils().showActivityIndicator(uiView: self.view)
  ViewControllerUtils().hideActivityIndicator(uiView: self.view)

有人知道我错过了什么吗?

【问题讨论】:

    标签: ios uitableview swift3 uiview


    【解决方案1】:

    每次如果你写ViewControllerUtils(),你正在创建 ViewControllerUtils 类的新实例,因此一旦你使用一个实例显示活动指示器,你必须获得相同的活动指示器实例来删除它你添加的它。因此,您可以将 ViewControllerUtils 设为单例类并使用它。

    class ViewControllerUtils {
        static let shared = ViewControllerUtils()
        private override init() {
        }
    }
    

    现在像这样调用你的方法..

    ViewControllerUtils.shared.showActivityIndicator(uiView: self.tableView)
    ViewControllerUtils.shared.hideActivityIndicator(uiView: self.tableView)
    

    【讨论】:

      猜你喜欢
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      相关资源
      最近更新 更多