使用id< protocol>在Interface Builder中的文件所有者? [英] Using id<protocol> for file's owner in Interface Builder?
问题描述
我有一个自定义的 UITableViewCell
我使用 instantiateWithOwner:(id)所有者选项:(NSDictionary *)选项从nib实例化
。实例化nib时,我将其保存到我的视图控制器中定义的IBOutlet,该控制器在.xib文件中设置为文件的所有者。一切都很好。
I have a custom UITableViewCell
that I am instantiating from a nib using instantiateWithOwner:(id)owner options:(NSDictionary *)options
. When the nib is instantiated, I am saving it to an IBOutlet defined in my view controller, which is set as the file's owner in the .xib file. Everything's been working great.
我现在遇到了在多个视图控制器中使用这个自定义单元的需要。我希望我可以定义一个协议(例如CustomCellOwner),多个视图控制器可以实现该协议。该协议将简单地定义用于在实例化时引用单元格的IBOutlet。
I've now come across the need to use this custom cell in multiple view controllers. I was hoping that I could define a protocol (e.g. CustomCellOwner), which multiple view controllers could implement. The protocol would simply define the IBOutlet used to reference the cell when instantiated.
理想情况下,我想将文件所有者设置为:
So ideally, I would like to set "file's owner" to:
id <CustomCellOwner>
。
但是, Interface Builder似乎只允许你将文件的所有者设置为已知类,而不是实现协议的id?
However, Interface Builder only seems to allow you to set file's owner to a known class, not to an id implementing a protocol?
有没有办法做到这一点?或者,更简单的方法来解决这个问题?
Is there any way to do this? Or, a simpler way to approach this problem?
谢谢!
推荐答案
这不是您要求的解决方案,但您可以创建一个 UIViewController
子类,为每个需要使用nib的视图控制器创建子类。类似于:
This isn't the solution you're asking for, but you could make a UIViewController
subclass that you subclass for each view controller that needs to use your nib. Something like:
@interface CustomCellOwnerViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIButton *someButton;
-(IBAction)doSomething;
@end
然后将其用作每个基类:
And then use that as the base class for each:
@interface FirstView : CustomCellOwnerViewController
然后您只需将文件所有者
设置为 CustomCellOwnerViewController
即可。
Then you could simply set File's Owner
to CustomCellOwnerViewController
with no problems.
只是一个想法。
这篇关于使用id< protocol>在Interface Builder中的文件所有者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!