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

标签 iphone ios ipad

我有一个显示一组缩略图的 View Controller ,最初它只显示 12,但我想允许更改它以允许不同的数字 9、6、4、2。

其中每一个都有不同的布局,所以我想加载不同的 XIB 但使用相同的 View Controller 类。所以我希望我可以通过传递一个参数来实现这一点,该参数可以让我知道在初始化时加载哪个 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。

最佳答案

这很简单,我会保留这个问题以防它对其他人有帮助。我像这样修改了我的 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 名称。这非常有效,并为我提供了我一直在寻找的灵 active 。

关于iphone - init ViewController 根据传入的参数加载不同的 XIB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9601894/

相关文章:

iPhone app 类似 Twitter 的设计结构

iphone - 某些 iphone sip 应用程序使用什么技巧与 UDP only sip 服务器通信?

ios - 如何使用 Xcode 制作 .framework 文件

iphone - 如何全屏显示ImageView?

ios - 在不使用全局变量和类的情况下在 IOS 中进行开发

javascript - 使用javascript检测ipad或iphone屏幕上的双击

ios - 检测图像 ios 上的黑线

ios - iOS Touches开始的后续触摸只有在第一触摸被移动时才能到达

iphone - Ipad/Iphone - UIView 中的图像旋转

iphone - 从后台返回时 AVCaptureSession 失败