【问题标题】:Cause viewDidLoad to run导致 viewDidLoad 运行
【发布时间】:2015-07-20 05:48:16
【问题描述】:
我正在创建一个新的UIViewController,但要等待几秒钟才能显示它。
问题是我希望在实际显示 now 对象之前调用 viewDidLoad。如何让viewDidLoad 运行?
目前我正在做这样的事情:
UIView *tempView = newObject.view;
但我真的不喜欢那个解决方案。
【问题讨论】:
标签:
objective-c
swift
uiview
uiviewcontroller
【解决方案1】:
如果有要在实例化视图时调用的要运行的代码,则只需将其放入 init 方法中即可。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName: nibNameOrNil
bundle: nibBundleOrNil];
if (self)
{
// Custom stuff
}
return self;
}
您希望调用viewDidLoad 还有其他原因吗?
【解决方案2】:
看起来只需调用 [newObject view]; 就可以了!
【解决方案3】:
- (void) viewDidLoad {
[super viewDidLoad];
self.view.alpha = 0; //When alpha = 0, it is also hidden
[UIView animateWithDuration:2.0f animations:^{
// Nothing to animate here
} completion:^(BOOL finished) {
// On animation completion show the content of the view, also animated.
[UIView animateWithDuration:0.5f animations:^{
self.view.alpha = 1;
}];
}];
}
【解决方案4】:
viewDidLoad 总是在对象显示在屏幕上之前调用。
如果你想要一些延迟,你可以在 viewDidLoad 或 viewDidAppear 中设置一些延迟代码。但是为什么您需要延迟,可能还有其他解决方案可以解决您的问题。