简体   繁体   中英

iOS - How to use a small view in different view controllers in Swift

I have a progress bar (with its own controller). This bar is supposed to be shown in different views depending on which view is visible. As the progress will be same, If possible I don't want to create many progress bar in many views rather I want to use same instance in all these views. Also in that way when I need to change any property of the progress bar it will be reflected commonly, which is required.

Please suggest me how can I use this common view. And also if my strategy is wrong, what would be the better design for such scenarios.

1) Well you have 2 options. You can declare a new Class ViewBox (or whatever name) and then use that inside your code

First View Controller

 var box:ViewBox = ViewBox()

When you segue or transition to your next screen, you can have a predefined variable var box:ViewBox! . Then say when you press a button, the button has a function called transition.

//Now setup the transition inside the Storyboard and name the identifier "toThirdViewController"
override func prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?) {
    if(segue.identifier == "toThirdViewController") {
        var vc = segue.destinationViewController as! `nextViewController` //The class of your next viewcontroller goes here
        vc.box = self.box
    }
    //Since The SecondViewController doesn't need ViewBox, we don't need it there.
}

where

nextViewController:UIViewController {
    var box:ViewBox!
}

Or you could do a much simpler way and that is to look up a UIPageViewController :)

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