Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove subview created for activityIndicator with swift

Tags:

ios

swift

uiview

i have created an activity window function which takes 3 inputs,

  • msg:String - the message which should show in the activity window
  • indicator:Bool - if the activity window should show or not
  • view:UIView - the uiview which should get the frame sizes etc from the View Controller which its called on

everything works fine, except the part where subview needs to be removed.if the same function is run on the main view controller. it works fine. just when i moved it to NSObject, it does not. please help

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

}
like image 964
Mugunthan Balakrishnan Avatar asked Dec 08 '25 07:12

Mugunthan Balakrishnan


1 Answers

move the variables out of the function like below

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

}
like image 124
Ranga Darshana Avatar answered Dec 10 '25 22:12

Ranga Darshana