I have a ViewController.xib
made in IB, that has a UITableView
. I want to build another xib-file e.g. SearchViewController.xib
, thas inherit from ViewController.xib
. The new SearchViewController.xib should inherit the UITableView
and additionally it should have a UISearchBar
and a UIButton
.
我有一个在IB中制作的ViewController.xib,它有一个UITableView。我想构建另一个xib文件,例如SearchViewController.xib,继承自ViewController.xib。新的SearchViewController.xib应该继承UITableView,另外它应该有一个UISearchBar和一个UIButton。
It's possible to make this scenario with the Interface Builder?
使用Interface Builder可以创建这个场景吗?
6 个解决方案
#1
10
No, there is no concept of inheritance among NIB files. Your only option is to duplicate the original file and make the necessary changes in the copy.
不,NIB文件中没有继承的概念。您唯一的选择是复制原始文件并在副本中进行必要的更改。
#2
4
You could make use of a Container View in order to reuse a particular view controller. That way you only have to create it once, add the logic once and you can simply reuse the component where-ever you like:
您可以使用Container View以重用特定的视图控制器。这样你只需要创建一次,添加一次逻辑,你可以简单地重用组件 - 你喜欢的地方:
This is Apples documentation on the Container View, hope it helps!
这是关于容器视图的Apples文档,希望它有所帮助!
#3
1
You'll need separate .xib files or, use one as the default, then add the controls (e.g. SearchViewController, etc) programmatically in code.
您需要单独的.xib文件,或者使用一个作为默认值,然后以编程方式在代码中添加控件(例如SearchViewController等)。
#4
1
The way to think about it is that a XIB file is a serialised object and not a class. An instance of a class (i.e. an object) cannot be subclassed, except from some prototype based languages like Javascript.
考虑它的方法是XIB文件是序列化对象而不是类。除了一些基于原型的语言(如Javascript)之外,类的实例(即对象)不能被子类化。
One way of mocking this however is by creating a parent class of UIView type which will load the XIB and add it to itself (remember that a XIB contains an NSArray of visual elements).
然而,模拟这种方法的一种方法是创建一个UIView类型的父类,它将加载XIB并将其添加到自身(请记住,XIB包含可视元素的NSArray)。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"TestView" owner:self options:nil];
UIView* mainView = (UIView*)[nibViews objectAtIndex:0];
self.label.text = @"Parent";
[self addSubview:mainView];
}
return self;
}
The Child can then simply override the initialiser as
然后,Child可以简单地覆盖初始化器
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.label.text = @"Child";
}
return self;
}
The self.label
is of course linked to a UILabel in the XIB file via interface builder and connected at runtime via the owner:self
argument.
self.label当然通过接口构建器链接到XIB文件中的UILabel,并在运行时通过owner:self参数连接。
#5
0
It's really a problem should be taken care of in xCode's next versions .As you need to write in code your interface needed to be inherited in other classes which is a nightmare in case of modifications are required later .Or you can copy/paste xib file which will be also a nightmare as you'll need to open them all and apply modifications
在xCode的下一个版本中应该注意这个问题。因为你需要在代码中编写你的接口需要在其他类中继承,这是一个噩梦,如果以后需要修改。或者你可以复制/粘贴xib文件这也将是一场噩梦,因为你需要打开它们并应用修改
#6
0
The problem is that subclass calls initWithNibName with it's class name.
问题是子类使用它的类名调用initWithNibName。
I would go with forcing superclass to load the superclass xib always even if inherited (make sure that outlets are in .h of the super class)
我会强迫超类加载超类xib,即使是继承的(确保出口在超类的.h中)
in superclass override initWithNib (it will be called as well as normal init if you created xib based vc)
在超类中覆盖initWithNib(如果你创建了基于xib的vc,它将被调用以及普通的init)
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:@"NameOfTheSuperClass" bundle:nibBundleOrNil];
if (self) {
}
return self;
}
#1
10
No, there is no concept of inheritance among NIB files. Your only option is to duplicate the original file and make the necessary changes in the copy.
不,NIB文件中没有继承的概念。您唯一的选择是复制原始文件并在副本中进行必要的更改。
#2
4
You could make use of a Container View in order to reuse a particular view controller. That way you only have to create it once, add the logic once and you can simply reuse the component where-ever you like:
您可以使用Container View以重用特定的视图控制器。这样你只需要创建一次,添加一次逻辑,你可以简单地重用组件 - 你喜欢的地方:
This is Apples documentation on the Container View, hope it helps!
这是关于容器视图的Apples文档,希望它有所帮助!
#3
1
You'll need separate .xib files or, use one as the default, then add the controls (e.g. SearchViewController, etc) programmatically in code.
您需要单独的.xib文件,或者使用一个作为默认值,然后以编程方式在代码中添加控件(例如SearchViewController等)。
#4
1
The way to think about it is that a XIB file is a serialised object and not a class. An instance of a class (i.e. an object) cannot be subclassed, except from some prototype based languages like Javascript.
考虑它的方法是XIB文件是序列化对象而不是类。除了一些基于原型的语言(如Javascript)之外,类的实例(即对象)不能被子类化。
One way of mocking this however is by creating a parent class of UIView type which will load the XIB and add it to itself (remember that a XIB contains an NSArray of visual elements).
然而,模拟这种方法的一种方法是创建一个UIView类型的父类,它将加载XIB并将其添加到自身(请记住,XIB包含可视元素的NSArray)。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"TestView" owner:self options:nil];
UIView* mainView = (UIView*)[nibViews objectAtIndex:0];
self.label.text = @"Parent";
[self addSubview:mainView];
}
return self;
}
The Child can then simply override the initialiser as
然后,Child可以简单地覆盖初始化器
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.label.text = @"Child";
}
return self;
}
The self.label
is of course linked to a UILabel in the XIB file via interface builder and connected at runtime via the owner:self
argument.
self.label当然通过接口构建器链接到XIB文件中的UILabel,并在运行时通过owner:self参数连接。
#5
0
It's really a problem should be taken care of in xCode's next versions .As you need to write in code your interface needed to be inherited in other classes which is a nightmare in case of modifications are required later .Or you can copy/paste xib file which will be also a nightmare as you'll need to open them all and apply modifications
在xCode的下一个版本中应该注意这个问题。因为你需要在代码中编写你的接口需要在其他类中继承,这是一个噩梦,如果以后需要修改。或者你可以复制/粘贴xib文件这也将是一场噩梦,因为你需要打开它们并应用修改
#6
0
The problem is that subclass calls initWithNibName with it's class name.
问题是子类使用它的类名调用initWithNibName。
I would go with forcing superclass to load the superclass xib always even if inherited (make sure that outlets are in .h of the super class)
我会强迫超类加载超类xib,即使是继承的(确保出口在超类的.h中)
in superclass override initWithNib (it will be called as well as normal init if you created xib based vc)
在超类中覆盖initWithNib(如果你创建了基于xib的vc,它将被调用以及普通的init)
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:@"NameOfTheSuperClass" bundle:nibBundleOrNil];
if (self) {
}
return self;
}