带有 Nib 文件和继承的 UIViewController

发布于 2024-10-13 11:22:26 字数 1379 浏览 4 评论 0原文

我正在尝试做一些非常棘手的事情,但我仍然停留在某个点上。我正在尝试使用从另一个带有其他 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: 应该调用超类,但它只调用自己的 nib 文件。在超类中,我尝试重写 initWithNibName:bundle: 方法并自己放置 nibName :

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

它只初始化并显示母类及其 IB 对象。我明白为什么,但我开始认为不可能做我想做的事。有什么建议吗?

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

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

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

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

问候,
KL94

I'm trying to do something really tricky and I'm still stuck at a point. I'm attempting to instance an UIViewController with a Nib file inherited from an other UIViewController with an other Nib file.
The problem is when I instantiate my son UIViewController .

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

The init method initWithNibName:bundle: should call the super class but it only call its own nib file. In the super class, I tried to override the initWithNibName:bundle: method and put the nibName myself like that :

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

It only init and display the Mother Class and its IB Object. I understand why but I begin thinking it is impossible to do what I want. Any suggestion ?

Edit:
I would use my SonViewController just like that :

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

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

It should display son and mother IB Object...

Regards,
kl94

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最好是你 2024-10-20 11:22:26

通常,出于这个原因,您应该只在 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;
}

Normally, you should only use a specific nib in the init method, and not the initWithNibName:bundle:, for this reason.

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

Then, to use the default nib for MotherViewController, you just use [[MotherViewController alloc] init];.

As an alternate, you could define a different initializer in MotherViewController for this reason.

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

Then, use a private category interface to tell SonViewController about this method.

//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;
}
定格我的天空 2024-10-20 11:22:26

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

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

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

I know this is an old thread, but I just found an incredibly blog post here.

Essentially, you have to iterate through all the views of the parent class and add them as subviews to your child class. Here's how I implemented it in my project:

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

That addSuviewsFromSuperclass method is not my coding. I have to give full credit to the author of the blogpost I mentioned above. Download his example project and you'll find it in his JMViewController.m.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文