开发者

How to create multiple windows with the same nib file in xcode

开发者 https://www.devze.com 2023-01-05 04:53 出处:网络
I have an iphone app t开发者_运维问答hat 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

I have an iphone app t开发者_运维问答hat 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.

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:

In the .h file:

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

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

}

In the .m file:

#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;
    }
}

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


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.


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


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.

Then, subclass your subclass to get customized functionality, like

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消