如何将 xib 视图快速加载到我的 viewController 中?
How can I load fast a xib view into my viewController?
我的 objective c 应用程序中有一个 xib。但是当我试图加载这个 viewController 时,这花费了大约 1 秒,这是一种糟糕的感觉......
我使用的是自定义字体,但我已将其正确添加到我的 .plist
我的 customView class 由文件所有者添加并与出口连接。
我的代码是:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
nibname=@"MyCustomView";
[self addSubview:
[[[NSBundle mainBundle] loadNibNamed:nibname
owner:self
options:nil] objectAtIndex:0]];
}
return self;
}
然后,我将它加载到我的 viewController 中,如下所示:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
...
MyCustomXib *myview = [[MyCustomXib alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
[self.view addSubview:myview];
...
}
我是否正确加载了这个自定义视图?
我怎样才能更快地完成这个加载过程?
编辑 1 更正然后阅读亚光评论:
[self.view addSubview:myview];
提供:上次错误代码:[self.view myview];
根据您所说的,我无法重现问题。我在您的 viewDidAppear
代码中添加了计时:
NSLog(@"%f", [[NSDate new] timeIntervalSinceReferenceDate]);
MyCustomXib *myview = [[MyCustomXib alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
[self.view addSubview:myview];
NSLog(@"%f", [[NSDate new] timeIntervalSinceReferenceDate]);
这是输出:
2017-01-24 12:47:18.812 NibLoadTest[12087:135969] 506983638.811982
2017-01-24 12:47:18.814 NibLoadTest[12087:135969] 506983638.814367
也就是说不是延迟1秒!这是几 千分之一 秒。
因此问题是虚幻的,或者是由您未显示的其他内容引起的。如果你想测量某件事花费了多长时间,测量它。理想情况下,使用 Instruments;这就是它的用途。它会说明什么需要时间 以及为什么 。请勿猜测。
我的 objective c 应用程序中有一个 xib。但是当我试图加载这个 viewController 时,这花费了大约 1 秒,这是一种糟糕的感觉......
我使用的是自定义字体,但我已将其正确添加到我的 .plist 我的 customView class 由文件所有者添加并与出口连接。
我的代码是:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
nibname=@"MyCustomView";
[self addSubview:
[[[NSBundle mainBundle] loadNibNamed:nibname
owner:self
options:nil] objectAtIndex:0]];
}
return self;
}
然后,我将它加载到我的 viewController 中,如下所示:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
...
MyCustomXib *myview = [[MyCustomXib alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
[self.view addSubview:myview];
...
}
我是否正确加载了这个自定义视图?
我怎样才能更快地完成这个加载过程?
编辑 1 更正然后阅读亚光评论:
[self.view addSubview:myview];
提供:上次错误代码:[self.view myview];
根据您所说的,我无法重现问题。我在您的 viewDidAppear
代码中添加了计时:
NSLog(@"%f", [[NSDate new] timeIntervalSinceReferenceDate]);
MyCustomXib *myview = [[MyCustomXib alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
[self.view addSubview:myview];
NSLog(@"%f", [[NSDate new] timeIntervalSinceReferenceDate]);
这是输出:
2017-01-24 12:47:18.812 NibLoadTest[12087:135969] 506983638.811982
2017-01-24 12:47:18.814 NibLoadTest[12087:135969] 506983638.814367
也就是说不是延迟1秒!这是几 千分之一 秒。
因此问题是虚幻的,或者是由您未显示的其他内容引起的。如果你想测量某件事花费了多长时间,测量它。理想情况下,使用 Instruments;这就是它的用途。它会说明什么需要时间 以及为什么 。请勿猜测。