简体   繁体   English

如何在2个ViewControllers Objective-C上添加UIView

[英]How to add a UIView on 2 ViewControllers Objective-C

TLDR; TLDR; How do I get the same UIView to be on 2 different ViewControllers? 如何在2个不同的ViewController上使用相同的UIView? Using Objective-C 使用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. 有一个按钮,当按下该按钮时会弹出一个UIView,该UIUI可以访问某些UISwitch以控制某些设置,然后在UIView上单击“确定”按钮以将其关闭。 I control the popup by making the alpha 1 when popping up and 0 when hiding. 我通过使弹出窗口的Alpha值为1,隐藏时为0来控制弹出窗口。

I need that UIView to be available on 2 different ViewControllers. 我需要该UIView在2个不同的ViewController上可用。 And the UISwitch state needs to be in sync for the 2 ViewControllers 而且UISwitch状态需要为2个ViewController同步

Thanks! 谢谢!

You can create XIb and load it on different View controllers 您可以创建XIb并将其加载到不同的View控制器上

I can help you in Swift. 我可以在Swift中为您提供帮助。

Step 1 - Create an XIB and assign a UIView to it. 第1步-创建一个XIB并为其分配一个UIView。

Step 2 - Call that View in your respective view controllers as below 第2步-如下所示在各自的视图控制器中调用该视图

    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 其中initSampleView函数的定义如下

    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. 有一个按钮,当按下该按钮时会弹出一个UIView,该UIUI可以访问某些UISwitch以控制某些设置,然后在UIView上单击“确定”按钮以将其关闭。 I control the popup by making the alpha 1 when popping up and 0 when hiding. 我通过使弹出窗口的Alpha值为1,隐藏时为0来控制弹出窗口。

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 而且UISwitch状态需要为2个ViewController同步

Write all needed info into UserDefaults as the user makes changes in the view, and read and obey it when the view controller appears. 当用户在视图中进行更改时,将所有必需的信息写入UserDefaults中,并在视图控制器出现时阅读并遵守。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM