2011-01-20 159 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