2011-01-20 24 views
5

Tôi đang cố gắng làm điều gì đó thực sự phức tạp và tôi vẫn bị mắc kẹt tại một điểm. Tôi đang cố gắng để ví dụ một UIViewController với một tập tin Nib thừa kế từ một UIViewController khác với một tập tin Nib khác.
Vấn đề là khi tôi khởi tạo con trai UIViewController.UIViewController với Nib File và Inheritance

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

Phương thức init initWithNibName:bundle: nên gọi super class nhưng nó chỉ gọi tập tin nib riêng của mình. Trong lớp siêu, tôi đã cố gắng để ghi đè lên các phương pháp initWithNibName:bundle: và đặt nibName bản thân mình như thế:

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

Nó chỉ init và hiển thị Mother Class và Object IB của nó. Tôi hiểu tại sao nhưng tôi bắt đầu nghĩ rằng không thể làm những gì tôi muốn. Bất kì lời đề nghị nào ?

Edit:
Tôi sẽ sử dụng SonViewController của tôi chỉ như thế:

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

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

Nó sẽ hiển thị con trai và mẹ IB Object ...

Kính trọng,
kl94

Trả lời

1

Tôi biết đây là một chủ đề cũ, nhưng tôi vừa tìm thấy một bài đăng blog cực kỳ độc đáo here.

Về cơ bản, bạn phải lặp qua tất cả các chế độ xem của lớp cha và thêm chúng dưới dạng xem phụ vào lớp con của bạn. Đây là cách tôi thực hiện nó trong dự án của tôi:

// 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 phương pháp không phải là mã hóa của tôi. Tôi phải cung cấp tín dụng đầy đủ cho tác giả của bài đăng trên blog mà tôi đã đề cập ở trên. Tải về dự án ví dụ của mình và bạn sẽ tìm thấy nó trong JMViewController.m của mình.

+0

Như bạn đã nói, đây là một bài đăng rất cũ. NHƯNG bây giờ, bởi vì bạn, tôi/Chúng tôi biết làm thế nào để tạo ra 2 bộ điều khiển xem với 2 xib và làm cho một trong những họ kế thừa từ khác. Cảm ơn – klefevre

3

Thông thường, bạn chỉ nên sử dụng một nib cụ thể trong phương thức init, chứ không phải initWithNibName: bundle :, vì lý do này.

@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]; 
} 

Sau đó, để sử dụng ngòi mặc định cho MotherViewController, bạn chỉ cần sử dụng [[MotherViewController alloc] init];.

Là một thay thế, bạn có thể xác định trình khởi tạo khác trong MotherViewController vì lý do này.

@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]; 
} 

Sau đó, sử dụng giao diện danh mục riêng để thông báo cho SonViewController về phương pháp này.

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

Tôi không quản lý nó để hoạt động. Tôi đã cập nhật bài viết của mình cho thấy tôi muốn sử dụng SonViewController – klefevre