我有一个自定义的 UITableViewCell
,我使用 instantiateWithOwner:(id)owner options:(NSDictionary *)options
从 nib 实例化它。当 Nib 被实例化时,我将它保存到我的 View Controller 中定义的 IBOutlet,它在 .xib 文件中被设置为文件的所有者。一切都很好。
我现在发现需要在多个 View Controller 中使用此自定义单元格。我希望我可以定义一个协议(protocol)(例如 CustomCellOwner),多个 View Controller 可以实现该协议(protocol)。该协议(protocol)将简单地定义用于在实例化时引用单元格的 IBOutlet。
理想情况下,我想将“文件的所有者”设置为:
id <CustomCellOwner>
在界面生成器中。
但是,Interface Builder 似乎只允许您将文件所有者设置为已知类,而不是实现协议(protocol)的 id?
有什么办法吗?或者,有更简单的方法来解决这个问题?
谢谢!
最佳答案
这不是您要求的解决方案,但您可以创建一个 UIViewController
子类,为每个需要使用您的 nib 的 View Controller 创建子类。像这样的东西:
@interface CustomCellOwnerViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIButton *someButton;
-(IBAction)doSomething;
@end
然后将其用作每个的基类:
@interface FirstView : CustomCellOwnerViewController
然后您可以简单地将 File's Owner
设置为 CustomCellOwnerViewController
,没有任何问题。
只是一个想法。
关于objective-c - 在 Interface Builder 中使用 id<protocol> 作为文件所有者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9368635/