用Swift移除为activityIndicator创建的子视图

3
我已经创建了一个活动窗口函数,它需要三个输入参数:
  • msg:String - 在活动窗口中显示的消息
  • indicator:Bool - 是否显示活动窗口
  • view:UIView - 从调用它的视图控制器获取框架大小等信息的UIView
除了需要删除子视图的部分之外,一切都正常。如果在主视图控制器上运行相同的函数,则可以正常工作。只是当我把它移到NSObject时,它就不能正常工作了。请帮忙解决。
class UIDesignFunction: NSObject {

func progressBarDisplayer(msg:String, indicator:Bool, view:UIView) {

    var activityIndicator = UIActivityIndicatorView()
    var strLabel = UILabel()
    var msgFrame = UIView()

    if indicator{

        //println(msg)
        strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
        strLabel.text = msg
        strLabel.textColor = UIColor.whiteColor()
        msgFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
        msgFrame.layer.cornerRadius = 15
        msgFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)

        activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
        activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        activityIndicator.startAnimating()
        msgFrame.addSubview(activityIndicator)
        UIApplication.sharedApplication().beginIgnoringInteractionEvents()
        msgFrame.addSubview(strLabel)
        view.addSubview(msgFrame)
    }else{
        UIApplication.sharedApplication().endIgnoringInteractionEvents()
        msgFrame.removeFromSuperview()
    }

}

mgFrame一直显示,无法关闭。 - Mugunthan Balakrishnan
willRemoveSubview没有起作用 :( 你如何使用removeFromSuperviewWithoutNeedingDisplay? - Mugunthan Balakrishnan
@MugunthanBalakrishnan 我理解的是,您首先使用 indicator = true 调用该函数以显示活动指示器,然后再次使用 indicator = false 调用该函数以删除活动指示器? - hennes
@ hennes 是的,没错 :) - Mugunthan Balakrishnan
1个回答

1
将变量从函数中移出,像下面这样。
class UIDesignFunction: NSObject {

var activityIndicator = UIActivityIndicatorView()
var strLabel = UILabel()
var msgFrame = UIView()

func progressBarDisplayer(msg:String, indicator:Bool, view:UIView) {

if indicator{

    //println(msg)
    strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
    strLabel.text = msg
    strLabel.textColor = UIColor.whiteColor()
    msgFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
    msgFrame.layer.cornerRadius = 15
    msgFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)

    activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
    activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
    activityIndicator.startAnimating()
    msgFrame.addSubview(activityIndicator)
    UIApplication.sharedApplication().beginIgnoringInteractionEvents()
    msgFrame.addSubview(strLabel)
    view.addSubview(msgFrame)
}else{
    UIApplication.sharedApplication().endIgnoringInteractionEvents()
    msgFrame.removeFromSuperview()
}

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接