代码之家  ›  专栏  ›  技术社区  ›  Enrico Susatyo

如何在Xcode中创建具有相同NIB文件的多个窗口

  •  4
  • Enrico Susatyo  · 技术社区  · 15 年前

    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文件。我在初始化文件时有此代码:

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

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

    3 回复  |  直到 14 年前
        1
  •  4
  •   Nickolay Olshevsky    14 年前

    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.

        2
  •  2
  •   Spire    14 年前

    If you put all your views in one NIB, then when your application launches, it has to load the entire NIB into memory and construct all of the objects for all of the controls, and this takes a lot of time.

    If you separate your views into separate NIBs, then your app will start up much faster, because only the NIB containing the initial view will be loaded. Then, when the view changes, you can load the NIB containing the new view. This will incur a minor hitch when opening a view for the first time.

    Also if you're trying to optimize memory usage, you can unload the precious view when switching to a view

        3
  •  0
  •   Ash Furrow    14 年前

    I'd suggest subclassing UITableViewController and add any methods, instance variables, and properties common to your different view here. Make your nib and have it have a reference to an instance of one of these objects.

    然后,对子类进行子类化以获得定制的功能,比如

    GenericSubclassVC* checkPrimeViewController = [[SpecificSubclassVC alloc] initWithNibName:@"GenericNib" and Bundle:nil];
    [self.navigationController pushViewController:checkPrimeViewController animated:YES];
    

    You should stick to overriding the designated initializer. Put any custom initialization code into awakeFromNib or viewDidLoad methods, depending on if they need to modify UIView objects.

    However, as others have mentioned, it's not terribly efficient or elegant. If all your ViewControllers are table view controllers and have the same data model to display, then there are other ways to get code reuse, like defining a datasource object.