Iphone 带Nib文件和继承的UIViewController
我正试图做一些非常棘手的事情,但我仍然被困在某个点上。我正在尝试使用从另一个具有另一个Nib文件的Iphone 带Nib文件和继承的UIViewController,iphone,objective-c,inheritance,uiviewcontroller,nib,Iphone,Objective C,Inheritance,Uiviewcontroller,Nib,我正试图做一些非常棘手的事情,但我仍然被困在某个点上。我正在尝试使用从另一个具有另一个Nib文件的UIViewController继承的Nib文件来实例一个UIViewController。 问题是当我实例化我的儿子时UIViewController // SonViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
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文件。在super类中,我尝试覆盖initWithNibName:bundle:
方法,并将nibName自己放在下面的位置:
// MotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:@"MotherViewController"
bundle:nibBundleOrNil])) {
// Custom initialization.
}
return self;
}
SonViewController *son = [[SonViewController alloc]
initWithNibName:@"SonViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:son animated:YES];
[son release];
它只初始化并显示母类
及其IB对象。我明白为什么,但我开始认为我想做的事是不可能的。有什么建议吗
编辑:我会像这样使用我的SonViewController:
// MotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:@"MotherViewController"
bundle:nibBundleOrNil])) {
// Custom initialization.
}
return self;
}
SonViewController *son = [[SonViewController alloc]
initWithNibName:@"SonViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:son animated:YES];
[son release];
它应该显示子和母IB对象
问候,kl94由于这个原因,通常情况下,您应该只在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];
}
@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];
}
然后,要使用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)init {
return [self initWithNibName:@"MotherViewController" bundle:nil];
}
@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;
}
我知道这是一篇老文章,但我刚刚发现了一篇令人难以置信的博文
本质上,您必须遍历父类的所有视图,并将它们作为子视图添加到子类中。以下是我如何在我的项目中实施它:
// 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中找到它。我没有设法让它工作。我更新了我的帖子,显示我想用我的SonViewControllerAs,正如你所说,这是一篇非常老的帖子。但是现在,因为您,我/我们知道如何使用2个xib创建2个视图控制器,并使其中一个从另一个继承。谢谢你