如何在xcode中使用相同的nib文件创建多个窗口 [英] How to create multiple windows with the same nib file in xcode

查看:77
本文介绍了如何在xcode中使用相同的nib文件创建多个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用表格视图作为界面的iPhone应用程序。每次用户点击其中一个表格单元格时,我想向用户显示另一个窗口。但是,我推入导航控制器的窗口的用户界面非常相似。因此,我决定在这个通用nib文件的文件所有者的所有子类中使用通用nib文件。

I have an iphone app that uses table view as the interface. Each time that the user taps on one of the table cells, I want to show another window to the user. However the user interface of the window that I push into the navigation controller are extremely similar. Therefore I decided to make a "generic nib file" to be used across all the subclasses of the file owner of this generic nib file.

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

However what I'm confused (and what's not working) is that I can't seem to be able to customise this generic nib file. I have this code at the initialisation of the files:

在.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;
}

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

But then when I instantiate the class in the table view:

//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文件?我不明白为什么我不能这样做,但我无法弄清楚如何......

So can anyone tell me what I'm doing wrong? Or am I wrong assuming that xcode allow me to use nib file multiple time across its subclasses? I don't see why I can't do it, but I can't figure out how...

推荐答案

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

When nib file is loaded, it creates view controller of exactly the same class that is written in .xib (UIViewController, or PrimeViewController, whatever else). What actually is saved in your .nib file? You'll succeed if you will store UIView and all the corresponding objects in xib, and will load only them by using [[NSBundle mainBundle] loadNibNamed:@"YourNibFile" owner:class1ViewController options:nil], while view in nib is connected to the corresponding File owner outlet.

这篇关于如何在xcode中使用相同的nib文件创建多个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆