我在XCode / iOS领域相对较新;我已经完成了一些不错的基于情节提要的应用程序,但是我从来没有在整个笔尖/ xib事情上咬牙切齿。我想对场景使用相同的工具来设计/布局可重复使用的视图/控件。因此,我为视图子类创建了第一个xib并对其进行了绘制:
正如我在情节提要中所做的那样,我已连接好插座并进行了约束设置。我将自己的类设置为File Owner
自定义UIView
子类的类。因此,我假设我可以使用一些API实例化此视图子类,并且它将如图所示进行配置/连接。
现在回到我的情节提要中,我想嵌入/重复使用此内容。我这样做是在表格视图原型单元格中:
我有一个观点。我已经将其类设置为我的子类。我已经为其创建了一个出口,以便可以对其进行操作。
$ 64的问题是,在哪里/如何指示仅将我的视图子类的空/未配置实例放在那里,还使用我创建的.xib对其进行配置/实例化还不够?如果在XCode6中可以输入用于给定UIView的XIB文件,那真是太酷了,但是我没有看到执行此操作的字段,因此我假设我必须在某个地方的代码中做些事情。
(我在SO上确实看到了类似的其他问题,但是还没有发现任何问题只是这个难题的一部分,或者是XCode6 / 2015的最新问题)
I am able to get this to kind of work by implementing my table cell's awakeFromNib
as follows:
- (void)awakeFromNib
{
// gather all of the constraints pointing to the uncofigured instance
NSArray* progressConstraints = [self.contentView.constraints filteredArrayUsingPredicate: [NSPredicate predicateWithBlock:^BOOL(id each, NSDictionary *_) {
return (((NSLayoutConstraint*)each).firstItem == self.progressControl) || (((NSLayoutConstraint*)each).secondItem == self.progressControl);
}]];
// fetch the fleshed out variant
ProgramProgressControl *fromXIB = [[[NSBundle mainBundle] loadNibNamed:@"ProgramProgressControl" owner:self options:nil] objectAtIndex:0];
// ape the current placeholder's frame
fromXIB.frame = self.progressControl.frame;
// now swap them
[UIView transitionFromView: self.progressControl toView: fromXIB duration: 0 options: 0 completion: nil];
// recreate all of the constraints, but for the new guy
for (NSLayoutConstraint *each in progressConstraints) {
id firstItem = each.firstItem == self.progressControl ? fromXIB : each.firstItem;
id secondItem = each.secondItem == self.progressControl ? fromXIB : each.secondItem;
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem: firstItem attribute: each.firstAttribute relatedBy: each.relation toItem: secondItem attribute: each.secondAttribute multiplier: each.multiplier constant: each.constant];
[self.contentView addConstraint: constraint];
}
// update our outlet
self.progressControl = fromXIB;
}
Is this as easy as it gets then? Or am I working too hard for this?
You're almost there. You need to override initWithCoder in your custom class you assigned the view to.
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"ViewYouCreated" owner:self options:nil] objectAtIndex:0]];
}
return self; }
Once that's done the StoryBoard will know to load the xib inside that UIView.
Here's a more detailed explanation:
This is how your UIViewController
looks like on your story board:
The blue space is basically a UIView that will "hold" your xib.
This is your xib:
There's an Action connected to a button on it that will print some text.
and this is the final result:
The difference between the first clickMe and the second is that the first was added to the UIViewController
using the StoryBoard
. The second was added using code.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句