2012-03-07 10 views
1

サムネイルのグループを表示するビューコントローラがあり、最初はちょうど12が表示されましたが、これを変更して別の番号、 4、2init ViewController渡されたパラメータに基づいて別のXIBをロードする

それぞれ異なるレイアウトになりますので、別のXIBをロードしたいのですが、同じView Controllerクラスを使用します。だから私はinitでロードするXIBを知らせるパラメータを渡すことでこれを達成できることを期待していました。私は、私は別のXIB年代をロード聞かせaPageSize上のスイッチのいくつかの並べ替えを使用することができますと仮定しています

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

はここに現在、私のinitです。

+0

なぜ毎回異なるnibでオブジェクトを作成しませんか? –

答えて

1

これはかなり簡単だった、I K他の人に役立つ場合に備えて質問を投稿してください。私はそうのように私のinitを変更:

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

インターファズビルダを使用して.xibのすべてを作成する必要はなく、インターファズビルダを使用してビューの一部を構築してから、viewDidLoadで残りのビューをサムネイルの数に応じて動的に作成できます

別のオプションは、diferentのケースでdiffierentのXIBをロードすることです:

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

それとも

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

私は理解していますが、現在の設定では機能しません。 – Slee

+0

他の回答で編集 –

関連する問題