UIViewController与Nib文件和继承

时间:2011-01-20 23:13:00

标签: iphone objective-c inheritance uiviewcontroller nib

我正在尝试做一些非常棘手的事情,而且我仍然陷入困境。我正在尝试使用从其他UIViewController继承的Nib文件和另一个Nib文件来实例UIViewController
问题是我实例化我的儿子UIViewController

// SonViewController
- (id)initWithNibName:(NSString *)nibNameOrNil 
                bundle:(NSBundle *)nibBundleOrNil
{
    if ((self = [super initWithNibName:nibNameOrNil 
                                bundle:nibBundleOrNil])) {
        // Custom initialization.
    }
    return self;
}

init方法initWithNibName:bundle:应该调用super class,但它只调用自己的nib文件。在超类中,我试图覆盖initWithNibName:bundle:方法并将nibName自己设置为:

// MotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil 
               bundle:(NSBundle *)nibBundleOrNil
{
    if ((self = [super initWithNibName:@"MotherViewController" 
                                bundle:nibBundleOrNil])) {
        // Custom initialization.
    }
    return self;
}

它只会初始化并显示Mother Class及其IB对象。我理解为什么,但我开始认为不可能做我想做的事。有什么建议吗?

编辑:
我会像我那样使用SonViewController:

SonViewController *son = [[SonViewController alloc] 
                      initWithNibName:@"SonViewController" bundle:[NSBundle mainBundle]];

[self.navigationController pushViewController:son animated:YES];
[son release];

它应该显示儿子和母亲IB对象......

的问候,
kl94

2 个答案:

答案 0 :(得分:3)

通常,您应该只在init方法中使用特定的nib,而不是initWithNibName:bundle:,因此。

@implementation MotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        //custom initialization
    }
    return self;
}
- (id)init {
    return [self initWithNibName:@"MotherViewController" bundle:nil];
}

然后,要使用MotherViewController的默认nib,只需使用[[MotherViewController alloc] init];

作为替代方案,您可以在MotherViewController中定义不同的初始化程序。

@implementation MotherViewController
- (id)_initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        //custom initialization
    }
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    return [self _initWithNibName:@"MotherViewController" bundle:nibBundleOrNil];
}

然后,使用私有类别接口告诉SonViewController有关此方法的信息。

//SonViewController.m
@interface MotherViewController (PrivateInit)
- (id)_initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
@end
@implementation SonViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        //custom initialization
    }
    return self;
}

答案 1 :(得分:1)

我知道这是一个老线程,但我刚刚发现了一篇令人难以置信的博文here

基本上,您必须遍历父类的所有视图,并将它们作为子视图添加到子类中。以下是我在项目中实现的方法:

// ChildViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addSubviewsFromSuperclass];  
}

// ParentViewController.h
- (void)addSubviewsFromSuperclass;   

// ParentViewController.m
- (void)addSubviewsFromSuperclass
{
    UIView *selfView = self.view;
    UIView *nibView = nil;
    @try
    {
        nibView = [NSBundle.mainBundle loadNibNamed:NSStringFromClass([self superclass]) owner:self options:nil][0];
    }
    @catch (NSException *exception)
    {
        NSLog(@"Something exceptional happened while loading nib:\n%@", exception);
    }
    self.view = selfView;
    for (UIView *view in nibView.subviews)
    {
        [self.view addSubview:view];
    }
}

addSuviewsFromSuperclass 方法不是我的编码。我必须完全赞赏我上面提到的博客文章的作者。下载他的示例项目,您将在他的JMViewController.m中找到它。

相关问题