[英]Can I initialize something in an iPhone app with a storyboard outside of viewDidLoad?
I have an iPhone application which consists of two View Controllers; 我有一个iPhone应用程序,它包含两个视图控制器; a main one, and one with the help screen.
一个主要的,一个带有帮助屏幕。 Each one has a button that performs the segue from one to the other.
每个人都有一个按钮,可以执行从一个到另一个的segue。
The problem I have is, when I segue back from the help screen to the main screen, the main view controller's viewDidLoad method gets called, so all of the initialization I did when the app was first started is repeated. 我遇到的问题是,当我从帮助屏幕回到主屏幕时,主视图控制器的viewDidLoad方法被调用,因此重复启动应用程序时所做的所有初始化。 Is there another method in the view controller that gets called just once, where I can do the initialization?
在视图控制器中是否有另一个方法只被调用一次,我可以在那里进行初始化?
My first thought was, "Have a boolean variable that is initially set to false, then have viewDidLoad test it, and if it is false, do the initialization, then set it to true" - but how do I initialize it to false in the first place? 我的第一个想法是,“有一个最初设置为false的布尔变量,然后让viewDidLoad测试它,如果它是false,则进行初始化,然后将其设置为true” - 但是如何将其初始化为false第一名?
My guess is that you're doing a "Push" segue (which is the most standard kind of segue one can do in an iOS app), and if you are using a "Push" segue that means you have a navigation controller in your app. 我的猜测是你正在做一个“Push”segue(这是在iOS应用程序中可以做的最标准的segue),如果你使用的是“Push”segue,这意味着你的导航控制器就在你的应用程序。
The best thing to do here is not to push another "Main" view controller onto the stack of other view controllers (which is why you are seeing " viewDidLoad
" called each time you push the main view), but instead when you click a "go to main" button in your help screen, pop the help screen off and return to the previous one. 这里最好的办法是不要将另一个“主”视图控制器推到其他视图控制器的堆栈上(这就是为什么每次推动主视图时都会看到“
viewDidLoad
”),而是当你点击“转到帮助屏幕中的主“按钮”, 弹出帮助屏幕并返回上一个屏幕。 The call that would do this is UINavigationController's popViewControllerAnimated
method. 执行此操作的调用是UINavigationController的
popViewControllerAnimated
方法。
Doing that means " viewDidLoad
" on that view controller only gets called once, as the main view gets loaded once. 这样做意味着该视图控制器上的“
viewDidLoad
”只会被调用一次,因为主视图会被加载一次。
on the .m class file create a bool on @implementaition
: 在.m类文件上创建一个
@implementaition
上的bool:
@implementation yourClass{
bool initialize = 0;
}
and then test it on view did load: 然后测试它在视图上加载:
-(void)viewDidLoad{
if(initialize == 0){
//do everything you need to do
initialize = 1;
}
}
I think it will work... 我认为它会起作用......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.