【问题标题】:Remove subview created for activityIndicator with swift使用 swift 删除为 activityIndi​​cator 创建的子视图
【发布时间】:2015-08-11 09:22:01
【问题描述】:

我创建了一个需要 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 一直显示并且不关闭
  • willRemoveSubview 不起作用 :( 你如何使用 removeFromSuperviewWithoutNeedingDisplay
  • @MugunthanBalakrishnan 我是否理解正确,您使用 indicator = true 调用该函数一次以显示活动指示器,然后再次使用 indicator = false 以删除活动指示器?
  • @hennes 是的,没错 :)

标签: ios swift uiview


【解决方案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()
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多