简体   繁体   中英

How to add a UIView on 2 ViewControllers Objective-C

TLDR; How do I get the same UIView to be on 2 different ViewControllers? Using Objective-C

I am making a small settings type option for an app.

There is a button which when pressed gives a popup(UIView) which gives access to some UISwitches to control some settings and then an "OK" button on the UIView to close it. I control the popup by making the alpha 1 when popping up and 0 when hiding.

I need that UIView to be available on 2 different ViewControllers. And the UISwitch state needs to be in sync for the 2 ViewControllers

Thanks!

You can create XIb and load it on different View controllers

I can help you in Swift.

Step 1 - Create an XIB and assign a UIView to it.

Step 2 - Call that View in your respective view controllers as below

    var sampleoneView: SampleView?

    var sampleTwoView: SampleView?

Initialise these views as below

    sampleoneView = SampleView.initSampleView(X vX:CGFloat, Y vY:CGFloat, width: CGFloat, height: CGFloat)
    sampleTwoView = SampleView.initSampleView(X vX:CGFloat, Y vY:CGFloat, width: CGFloat, height: CGFloat)

where the initSampleView func will be defined as below

    public class func initSampleView() -> SampleView{
    let sampleView:SampleView = Bundle.main.loadNibNamed("SampleView", owner: self, options: nil)?.last as! SampleView
    sampleView.frame = CGRect(x: vX, y: vY, width: width, height: height)
    return sampleView
    }

There is a button which when pressed gives a popup(UIView) which gives access to some UISwitches to control some settings and then an "OK" button on the UIView to close it. I control the popup by making the alpha 1 when popping up and 0 when hiding.

So this is not just a view and its subviews but a whole set of controller behaviors that operate upon them. Thus this needs to be a child view controller instantiated with its view twice. Use a Container View in the storyboard in both places, with the Embed segues leading to same view controller and its view.

And the UISwitch state needs to be in sync for the 2 ViewControllers

Write all needed info into UserDefaults as the user makes changes in the view, and read and obey it when the view controller appears.

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