Iphone init ViewController根据传入的参数加载不同的XIB

Iphone init ViewController根据传入的参数加载不同的XIB,iphone,ios,ipad,Iphone,Ios,Ipad,我有一个视图控制器,它显示一组缩略图,最初它只显示了12个,但我想允许对其进行更改,以允许不同的数字,9、6、4、2 每个都有不同的布局,所以我想加载不同的XIB,但使用相同的视图控制器类。所以我希望通过传入一个参数来实现这一点,该参数可以让我知道在init上加载哪个XIB 以下是我当前的初始值: -(id) initWithPriceLevel: (NSNumber *) aPriceLevel withLabelTemplate:(NSString *) aLabelTemplate wit

我有一个视图控制器,它显示一组缩略图,最初它只显示了12个,但我想允许对其进行更改,以允许不同的数字,9、6、4、2

每个都有不同的布局,所以我想加载不同的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。

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

另一种选择是在不同情况下加载不同的xib:

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


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

另一种选择是在不同情况下加载不同的xib:

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


这很简单,我会把问题贴出来,以防对其他人有帮助。我修改了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名称。这很有效,给了我我一直在寻找的灵活性。

这很简单,我会把问题贴出来,以防对其他人有所帮助。我修改了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名称。这非常有效,给了我我一直在寻找的灵活性。

为什么不每次创建具有不同笔尖的对象?为什么不每次创建具有不同笔尖的对象?