【问题标题】:UIViewController with Nib File and Inheritance带有 Nib 文件和继承的 UIViewController
【发布时间】:2011-06-12 19:54:52
【问题描述】:

我正在尝试做一些非常棘手的事情,但我仍然卡在一个点上。我正在尝试使用另一个 Nib 文件继承自另一个 UIViewController 的 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

【问题讨论】:

    标签: iphone objective-c inheritance uiviewcontroller nib


    【解决方案1】:

    通常,您应该只在 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;
    }
    

    【讨论】:

    • 我没能成功。我已经更新了我的帖子以显示我想使用我的 SonViewController
    【解决方案2】:

    我知道这是一个老帖子,但我刚刚发现了一篇令人难以置信的博文 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 中找到它。

    【讨论】:

    • 正如你所说,这是一篇很老的帖子。但是现在,因为你,我/我们知道如何用 2 个 xib 创建 2 个视图控制器,并使它们从另一个继承。谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2023-03-26
    • 1970-01-01
    • 2013-03-27
    相关资源
    最近更新 更多