Ios 从xib加载自定义UIView
我有一个自定义的Ios 从xib加载自定义UIView,ios,objective-c,iphone,uiview,xib,Ios,Objective C,Iphone,Uiview,Xib,我有一个自定义的ui视图,我正在以这种方式加载它 - (id)initWithFrame:(CGRect)frame withDelegate:(id)del { self = [super initWithFrame:frame]; UIView *myView; if (self) { NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self
ui视图
,我正在以这种方式加载它
- (id)initWithFrame:(CGRect)frame withDelegate:(id)del
{
self = [super initWithFrame:frame];
UIView *myView;
if (self)
{
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
owner:self
options:nil];
for (id object in nibViews)
{
if ([object isKindOfClass:[self class]])
myView = object;
}
myView.frame = frame;
myView.backgroundColor = [UIColor clearColor];
[self addSubview: myView];
}
self.delegate = del;
return self;
}
当UIView方法返回
self
时,像这样加载视图时,Interface Builder
中链接的所有插座都是nil
。这不是正确的方法吗?使用此方法从束中加载特定的xib
UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
使用此方法从捆中加载特定的xib
UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
试试这个:
self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
owner:self
options:nil] objectAtIndex:0];
试试这个:
self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
owner:self
options:nil] objectAtIndex:0];
用这个
myView =[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil].firstObject];
或者你可以使用简单的
myView = [nibViews firstObject]
用这个
myView =[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil].firstObject];
或者你可以使用简单的
myView = [nibViews firstObject]
据我所知,您在xid中提供了IBOutlet,当您尝试像提供这些值一样加载视图时,这些值为nil。这是预期的,因为您创建了一个新的viwe并将来自xib的视图添加为子视图。因此,parrent视图不会设置此值。 要解决这个问题,您需要在加载视图时重新编写方法。 请尝试使用此选项:
+ (id)createWithFrame:(CGRect)frame withDelegate:(id)del
{
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
owner:nil
options:nil];
<Provide class name> *myView = nibViews[0];
myView.frame = frame;
myView.backgroundColor = [UIColor clearColor];
myView.delegate = del;
return myView;
}
+(id)createWithFrame:(CGRect)frame with delegate:(id)del
{
NSArray*nibViews=[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class])
业主:无
选项:无];
*myView=nibViews[0];
myView.frame=frame;
myView.backgroundColor=[UIColor clearColor];
myView.delegate=del;
返回myView;
}
据我所知,您在xid中提供了IBOutlet,当您尝试加载视图时,这些值为零。这是预期的,因为您创建了一个新的viwe并将来自xib的视图添加为子视图。因此,parrent视图不会设置此值。
要解决这个问题,您需要在加载视图时重新编写方法。
请尝试使用此选项:
+ (id)createWithFrame:(CGRect)frame withDelegate:(id)del
{
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
owner:nil
options:nil];
<Provide class name> *myView = nibViews[0];
myView.frame = frame;
myView.backgroundColor = [UIColor clearColor];
myView.delegate = del;
return myView;
}
+(id)createWithFrame:(CGRect)frame with delegate:(id)del
{
NSArray*nibViews=[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class])
业主:无
选项:无];
*myView=nibViews[0];
myView.frame=frame;
myView.backgroundColor=[UIColor clearColor];
myView.delegate=del;
返回myView;
}
循环不需要
只需使用myView=[nibViews firstObject]代码>可能重复的请尽可能共享演示代码。可能重复的您不需要for loop
只需使用myView=[nibViews firstObject]代码>可能重复的请尽可能共享演示代码。可能重复的