2011-01-20 102 views
5

我试图做一些非常棘手的事情,我仍然坚持一点。我试图用其他UIViewController与另一个Nib文件继承的Nib文件实例化一个UIViewController
问题是当我实例化我的儿子UIViewControllerUIViewController与笔尖文件和继承

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

init方法initWithNibName:bundle:应该叫super class但只叫了自己的笔尖文件。在超类中,我试图重写initWithNibName:bundle:方法,并把自己nibName这样的:

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

它仅init和显示Mother Class和IB对象。我明白为什么,但我开始认为不可能做我想做的事。任何建议?

编辑:
我会用我的SonViewController就像这样:

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

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

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

问候,
kl94

回答

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中找到它。

+0

正如你所说,这是一个非常旧的帖子。但是现在,因为你,我/我们知道如何用2个xib创建2个视图控制器,并让它们从另一个继承。谢谢你 – klefevre 2014-04-17 10:06:32

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,你只需要使用[[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; 
} 
+0

我没有管理它的工作。我已更新我的帖子以显示我想使用我的SonViewController – klefevre 2011-01-20 23:57:35