iphone – 如何在xcode中使用相同的nib文件创建多个窗口

前端之家收集整理的这篇文章主要介绍了iphone – 如何在xcode中使用相同的nib文件创建多个窗口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用表视图作为界面的iPhone应用程序.每次用户点击其中一个表格单元格时,我想向用户显示另一个窗口.然而,我推入导航控制器的窗口的用户界面非常相似.因此,我决定制作一个“通用nib文件”,用于此通用nib文件文件所有者的所有子类.

然而,我感到困惑(以及什么不起作用)是我似乎无法自定义这个通用的nib文件.我在初始化文件时有这个代码

在.h文件中:

#import <UIKit/UIKit.h>
#import "primeViewController.h"

@interface subclass1 : primeViewController { //subclassing from a generic view controller that owns a generic nib file

}

在.m文件中:

#import "subclass1.h"

@implementation subclass1

- (id) initWithLabelAndButton {
    if(self = [super initWithNibName:@"primeViewController" bundle:nil]) {
        self.label.text = @"Title of the first subclass";   

    }
    return self;
}

但是当我在表视图中实例化类时:

//somewhere in rootviewcontroller.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.row) {
        case 0:
        {
            checkPrime* checkPrimeViewController = [[checkPrime alloc] initWithLabelAndButton];
            [self.navigationController pushViewController:checkPrimeViewController animated:YES];
            [checkPrimeViewController release];
            break;
        }
        default:
            break;
    }
}

所以有人能告诉我我做错了什么吗?或者我错了,假设xcode允许我在其子类中多次使用nib文件?我不明白为什么我不能这样做,但我无法弄清楚如何……

解决方法

当加载nib文件时,它会创建与.xib(UIViewController或PrimeViewController,无论其他)编写的完全相同的类的视图控制器.实际保存在.nib文件中的是什么? 你将成功存储UIView和xib中的所有相应对象,并且只使用[[NSBundle mainBundle] loadNibNamed:@“YourNibFile”所有者:class1ViewController选项:nil]加载它们,而nib中的视图连接到相应的文件所有者插座.

猜你在找的Xcode相关文章