2012-03-07 44 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

相關問題