I've created some custom UITableViewCells
in a nib file and would like to use that across multiple UIViewControllers
.
我已经在nib文件中创建了一些自定义的uitableviewcell,并希望跨多个uiviewcontroller来使用它。
Can anyone tell me the best practice way to do that? My limited knowledge around loading nibs seems to suggest that you have to specify a single owner class in Interface Builder.
谁能告诉我最好的练习方法吗?我在加载nibs方面的有限知识似乎表明,您必须在Interface Builder中指定单个所有者类。
Thanks.
谢谢。
2 个解决方案
#1
2
Just do it; there are no real obstacles. As for the class for File's Owner, so long as the keys you bind to in the nib exist in any object you want to use the nib in, you will be ok.
想做就做;没有真正的障碍。至于File's Owner的类,只要您在nib中绑定的键存在于您想要使用nib的任何对象中,就没有问题。
#2
1
You can instantiate a UIViewController
class programmatically with initWithNibName:bundle:
and specify the same nib for multiple controllers. For example, I'm building a game right now that has a GameController
class that defines the basic game logic in it. The GameController
has an associated GameController.xib
file that gets loaded up in a custom initializer:
您可以用initWithNibName:bundle:以编程方式实例化一个UIViewController类,并为多个控制器指定相同的nib。例如,我正在构建一个游戏,它有一个GameController类,定义了游戏的基本逻辑。GameController有一个相关的GameController。在自定义初始化器中加载的xib文件:
- (id)initWithOptions:(NSDictionary *)gameOptions
{
if (self = [super initWithNibName:@"GameController" bundle:nil])
{
// code here
}
return self;
}
I have a few different game types: peer-to-peer, local, & online. The game logic is the same, but the communication implementation is slightly different, so each of these is a subclass of GameController
. Based on how the application is used, it'll instantiate a different controller:
我有一些不同的游戏类型:点对点、本地和在线。游戏逻辑是相同的,但是通信实现略有不同,因此每个都是GameController的子类。根据应用程序的使用方式,它将实例化一个不同的控制器:
local = [[LocalGameController alloc] initWithOptions:options];
online = [[OnlineGameController alloc] initWithOptions:options];
You can see though that since these both extend GameController
, that both of these will be init'ed with GameController.xib
as its view. In this case, GameController
would be your single file owner.
您可以看到,由于这两个都扩展了GameController,所以这两个都将使用GameController来初始化。xib的观点。在这种情况下,GameController将是您的单个文件所有者。
#1
2
Just do it; there are no real obstacles. As for the class for File's Owner, so long as the keys you bind to in the nib exist in any object you want to use the nib in, you will be ok.
想做就做;没有真正的障碍。至于File's Owner的类,只要您在nib中绑定的键存在于您想要使用nib的任何对象中,就没有问题。
#2
1
You can instantiate a UIViewController
class programmatically with initWithNibName:bundle:
and specify the same nib for multiple controllers. For example, I'm building a game right now that has a GameController
class that defines the basic game logic in it. The GameController
has an associated GameController.xib
file that gets loaded up in a custom initializer:
您可以用initWithNibName:bundle:以编程方式实例化一个UIViewController类,并为多个控制器指定相同的nib。例如,我正在构建一个游戏,它有一个GameController类,定义了游戏的基本逻辑。GameController有一个相关的GameController。在自定义初始化器中加载的xib文件:
- (id)initWithOptions:(NSDictionary *)gameOptions
{
if (self = [super initWithNibName:@"GameController" bundle:nil])
{
// code here
}
return self;
}
I have a few different game types: peer-to-peer, local, & online. The game logic is the same, but the communication implementation is slightly different, so each of these is a subclass of GameController
. Based on how the application is used, it'll instantiate a different controller:
我有一些不同的游戏类型:点对点、本地和在线。游戏逻辑是相同的,但是通信实现略有不同,因此每个都是GameController的子类。根据应用程序的使用方式,它将实例化一个不同的控制器:
local = [[LocalGameController alloc] initWithOptions:options];
online = [[OnlineGameController alloc] initWithOptions:options];
You can see though that since these both extend GameController
, that both of these will be init'ed with GameController.xib
as its view. In this case, GameController
would be your single file owner.
您可以看到,由于这两个都扩展了GameController,所以这两个都将使用GameController来初始化。xib的观点。在这种情况下,GameController将是您的单个文件所有者。