我有一个显示一组缩略图的视图控制器,最初它只显示12但我想允许更改它以允许不同的数字,9,6,4,2。
每个都有不同的布局,所以我想加载一个不同的XIB但使用相同的视图控制器类。所以我希望我可以通过传入一个参数来完成这个,这个参数可以让我知道在init上加载哪个XIB。
这是我的init:
-(id) initWithPriceLevel: (NSNumber *) aPriceLevel withLabelTemplate:(NSString *) aLabelTemplate withPageSize: (int) aPageSize {
self = [self init];
if (self) {
self.priceLevel = aPriceLevel;
self.labelTemplate = aLabelTemplate;
if ([aPriceLevel isEqualToNumber:[NSNumber numberWithInt:0]]) {
self.key = @"BasePrice";
} else {
self.key = [NSString stringWithFormat: @"PriceLevel%@", aPriceLevel];
}
queue = dispatch_queue_create("com.myapp.thumbnailimages", NULL);
}
return self;
}
我假设我可以在aPageSize
上使用某种开关来加载不同的XIB。
答案 0 :(得分:1)
这很简单,我保留发布的问题以防万一。我这样修改了我的初始化:
-(id) initWithPriceLevel: (NSNumber *) aPriceLevel withLabelTemplate:(NSString *) aLabelTemplate withNibName:(NSString *) aNibName {
if ([aNibName isEqualToString:@""]) {
aNibName = @"PageCollectionViewController";
}
self = [self initWithNibName:aNibName bundle:nil];
if (self) {
self.priceLevel = aPriceLevel;
self.labelTemplate = aLabelTemplate;
if ([aPriceLevel isEqualToNumber:[NSNumber numberWithInt:0]]) {
self.key = @"BasePrice";
} else {
self.key = [NSString stringWithFormat: @"PriceLevel%@", aPriceLevel];
}
queue = dispatch_queue_create("com.myapp.thumbnailimages", NULL);
}
return self;
}
我添加了传递NIB名称的参数,如果它只是一个空字符串,我使用默认的NIB名称。这很有效,给了我正在寻找的灵活性。
答案 1 :(得分:0)
您不需要使用interfaz构建器在.xib中创建所有内容,您可以使用interfaz构建器构建部分视图,然后在viewDidLoad中根据缩略图的数量动态创建其余视图< / p>
另一个选择是在不同的情况下加载不同的xib:
MyClass *myClass = [[MyClass alloc] initWithNibName:@"my1xib" bundle:nil];
或者
MyClass *myClass = [[MyClass alloc] initWithNibName:@"my2xib" bundle:nil];