简体   繁体   中英

removeFromSuperview() doesn't work

I have the following class to add and remove an activity indicator view:

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))
}

}

I call the function in tableview and in view as well in the following way:

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

    invitedEvents()
}

After loading the data I try to remove the activity indicator, but it is not removing.

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

When the tableview is loading is printing that hideActivityIndicator was called, but it not removing from the view.

I call the functions in a view as well and the same result: showActivityIndicator is working, hideActivityIndicator doesn't work.

I call the functions in a view with the following parameters:

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

Does anybody an idea what I'm missing?

Everytime if you write ViewControllerUtils() , you are creating the new instance of ViewControllerUtils class and hence once you show the activity indicator using one instance , you have to get the same instance of activity indicator to remove it with which you have added it. So you can make the ViewControllerUtils as singleton class and hence use it.

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

Now call your methods like this ..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM