简体   繁体   中英

Can we get an array of all storyboard views

I'm developping application in Swift. This application has many view and I would like to put a UIProgressView on all views

Can we get an array of all storyboard views ?

for exemple :

    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 you

Ysée

I assume that what you really want is to have the progress displayed on every view IF there is an operation in progress.

There are many ways to do that (using delegation, NSNotificationCenter, …) but the easiest I can think of would be to rely on viewWillAppear

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

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

For the user, it will effectively look like you added the progress view to all views.

Why not create a base class that has a lazy stored property of type UIProgressView ? Optionally you can have two methods setProgressViewHidden(hidden : Bool) in order to easily show and hide the progress view and setProgress(progress : Float) to update the progress. Then all your view controllers can subclass this base class and conveniently interact 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

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