问题描述
我只是想在Objective C和IOS中探讨MVC,但我遇到了一个问题,我希望有人可以帮助我。
I am just trying to get my head around MVC in Objective C and IOS but am having a problem I'm hoping someone can help me with.
我有创建了一个自定义视图(在XIB中创建为子UIView),该视图使用简单的委托协议从drawRect中的委托中请求信息。我有一个视图控制器,它实现协议并通过接口构建器连接到视图。
自定义视图还有一些我想在启动时设置的属性。
我遇到的问题是弄清楚控制器应该如何访问视图以设置这些属性,因为它似乎没有直接访问它。此外,这些属性似乎在界面构建器检查器中不可见,正如我所期望的那样,与我添加的委托属性不同。
I have created a custom view (created as a child UIView in a XIB) that uses a simple delegate protocol for requesting information from its delegate in drawRect. I have a view Controller that implementes the protocol and is connected to the view through interface builder.The custom view also has a few properties that I want to set on startup.The problem I have is working out how the controller is supposed to access the view to set these properties as it doesn't appear to have direct access to it. Also the properties don't seem to be visible in interface builder inspector as I would expect unlike the delegate property I added.
最初我认为我可以做类似的事情
Initially I thought I could do something like
[self.view setViewIntProperty:10]
但是这将调用主XIB视图,我的自定义视图实际上是此视图的子视图,所以我需要一些特定的子视图,我可以从viewDidLoad中的控制器初始化它。
But that would be calling the main XIB view and my custom view is actually a child of this view so I need someway to get that specific child view to I can initialise it from the controller in viewDidLoad.
希望这很清楚。我确信这应该很容易,我错过了一些简单的东西,但看不出它应该如何正常工作。
Hopefully that is clear. I'm sure this should be easy and I've missed something simple but can't see how this should normally work.
推荐答案
您的自定义视图的属性(而不是其出口)只能在代码中设置,除非您创建。
Your custom view's properties (as opposed to its outlets) can only be set in code unless you create an IB plugin for it.
如果您在控制器中为每个子视图创建一个插座,则可以轻松访问其他子视图。视图插座作为该视图控制器的主视图。没有什么可以阻止您为其他视图/控件创建其他插座。 Yu只需要子视图视图控制器并根据需要添加插座。只需记住将控制器的类名(在Interface Builder中)设置为自定义子类的类名。这将揭示您可以连接的可用插座。
Your other subviews can be accessed easily if you create an outlet for each of them in your controller. The view outlet is there as the primary view of that view controller. There is nothing preventing you creating additional outlets to other views/controls. Yu would just need to subclass the view controller and add the outlets as needed. Just remember to set the class name of the controller (in Interface Builder) to that of your custom subclass. That will expose the available outlets for you to connect.
如果要在IB的检查器调色板中使控件的自定义属性可用,则仍需要创建Interface Builder插件。除非您计划在其他应用程序中频繁地重复使用它或将其提供给其他应用程序,否则最简单的方法就是在源代码中设置属性。
You'd still need to create an Interface Builder plugin if you want to make your control's custom properties available in IB's inspector palette. Unless you plan to reuse it frequently in other applications or make it available to others, it's probably easiest just to set the properties in your source code.
这篇关于如何在从XIB实例化的自定义视图上设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!