Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios removeFromSuperview()不';行不通_Ios_Uitableview_Swift3_Uiview - Fatal编程技术网

Ios removeFromSuperview()不';行不通

Ios removeFromSuperview()不';行不通,ios,uitableview,swift3,uiview,Ios,Uitableview,Swift3,Uiview,我有以下类来添加和删除活动指示器视图: import Foundation import UIKit class ViewControllerUtils { var container: UIView = UIView() var loadingView: UIView = UIView() var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() func showActivityIndicato

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

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和in 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正在加载打印时,调用了hideActivityIndicator,但它没有从视图中删除

我也在视图中调用了这些函数,结果是一样的:showActivityIndicator正在工作,hideActivityIndicator不工作

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

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

有人知道我遗漏了什么吗?

每次编写
ViewControllerUtils()
时,您都在创建ViewControllerUtils类的新实例,因此,一旦使用一个实例显示活动指示器,您就必须获得与添加它相同的活动指示器实例来删除它。因此,您可以将
ViewControllerUtils
设置为singleton类,从而使用它

class ViewControllerUtils {
    static let shared = ViewControllerUtils()
    private override init() {
    }
}
现在像这样调用您的方法

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

每次编写
ViewControllerUtils()
时,您都在创建ViewControllerUtils类的新实例,因此,一旦使用一个实例显示活动指示器,您就必须获得与添加该指示器相同的活动指示器实例以将其删除。因此,您可以将
ViewControllerUtils
设置为singleton类,从而使用它

class ViewControllerUtils {
    static let shared = ViewControllerUtils()
    private override init() {
    }
}
现在像这样调用您的方法

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