2012-03-07 46 views
1

我有一个视图控制器显示一组缩略图,最初它只显示了12,但我想允许更改这个允许不同的数字,9,6, 4,2.init ViewController根据传递的参数加载不同的XIB

这些都将有不同的布局,所以我想加载不同的XIB,但使用相同的视图控制器类。所以我希望我可以通过传入一个参数让我知道在init上加载哪个XIB。

这是目前我的初始化:

-(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

你为什么不每次都用不同的笔尖创建一个对象? – 2012-03-07 13:07:20

回答

1

这是非常简单的,I K把这个问题贴出来,以防别人帮助其他人。我修改初始化像这样:

-(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的名字。这工作很好,给了我寻找的灵活性。

0

您不需要使用interfaz构建器创建.xib中的所有内容,可以使用interfaz构建器构建视图的一部分,然后在viewDidLoad中根据缩略图的数量动态创建其余视图

另一种选择是加载在diferent情况下diffierent厦门国际银行:

MyClass *myClass = [[MyClass alloc] initWithNibName:@"my1xib" bundle:nil]; 

或者

MyClass *myClass = [[MyClass alloc] initWithNibName:@"my2xib" bundle:nil]; 
+0

我明白,但它不适用于我目前的设置 – Slee 2012-03-07 13:30:33

+0

编辑与其他答案 – 2012-03-07 13:33:21

相关问题