Can we get an array of all the views of the storyboard

I am developing an application in Swift. This app has a lot of views and I would like to put the UIProgressView in all views

Is it possible to get an array of all the views of a storyboard?

for example:

    self.progressBar = UIProgressView(progressViewStyle: .Bar)
    self.progressBar?.center = view.center
    self.progressBar?.frame = CGRect(x: 0, y: 20, width: view.frame.width, height: CGFloat(1))
    self.progressBar?.progress = 1/2
    self.progressBar?.trackTintColor = UIColor.lightGrayColor();
    self.progressBar?.tintColor = UIColor.redColor();

    var arrayViewController : [UIViewController] = [...,...,...]

    for controller in arrayViewController {
        controller.view.addSubview(self.progressBar)
    }

      

thank

YsΓ©e

+3


source to share


2 answers


I am assuming that you really want the progress to be displayed in all views if an operation is in progress.

There are many ways to do this (using delegation, NSNotificationCenter, ...), but the easiest one I can think of is relying on viewWillAppear



override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    // Check if there an operation in progress and add progressView if relevant
}

      

To the user, it will look like you've added a progress view to all views.

0


source


Why not create a base class from a lazy stored property

type UIProgressView

? Optionally, you can use two methods setProgressViewHidden(hidden : Bool)

to easily show and hide the progress view and setProgress(progress : Float)

to update the progress. Then all of your view controllers can subclass this base class and interact conveniently with the progress view.

    class ProgressViewController : UIViewController {

         lazy var progressView : UIProgressView = {
            [unowned self] in

            var view = UIProgressView(frame: CGRectMake(0, 20, self.view.frame.size.width, 3))
            view.progress = 0.5
            view.trackTintColor = UIColor.lightGrayColor()
            view.tintColor = UIColor.redColor()

            self.view.addSubview(view)

            return view
        }()
    }

      



To read more about lazy stored properties check: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html

0


source







All Articles