简体   繁体   English

viewDidLoad中的代码每次调用时都会运行

[英]Code in viewDidLoad runs every time it is called

Hi all I am doing a course in Udemy, and the code calls for placing code in the viewDidLoad function as shown below: 大家好我正在Udemy做一个课程,代码要求在viewDidLoad函数中放置代码,如下所示:

    override func viewDidLoad() {
    super.viewDidLoad()

    placesArray.append(["name":"Taj Mahal", "lat":"27.175607", "lon":"78.042112"])

}

The array append should only run once, however, when I segue to another viewController and come back, it runs the code to append again. 数组追加应该只运行一次,但是,当我转到另一个viewController并返回时,它运行代码再次追加。 So I now have an array with 2 rows, both of which are Taj Mahal. 所以我现在有一个有2行的数组,这两行都是Taj Mahal。

I thought that the viewDidLoad function only runs code once? 我以为viewDidLoad函数只运行一次代码?

Is there a way around this? 有没有解决的办法?

Thanks. 谢谢。

Addendum: I am using Swift, so I don't see any alloc and init while creating and launching the viewController. 附录:我使用的是Swift,所以在创建和启动viewController时我没有看到任何alloc和init。 And weird as it sounds, the video tutorial has it working in the viewDidLoad and the trainer is using the storyboard to segue from the initial table view controller to a map view on a view controller and just has a back button on the map view that segue's back to the table view controller via the storyboard as well. 听起来很奇怪,视频教程让它在viewDidLoad中工作,教练正在使用故事板从初始表视图控制器转到视图控制器上的地图视图,并且在地图视图上只有一个后退按钮通过故事板返回到表视图控制器。 - Could be because I have the latest version of the Swift language and the trainer was using an earlier version, (cause I noticed some slight differences in coding earlier) but you never know. - 可能是因为我有最新版本的Swift语言而且教练使用的是早期版本,(因为我注意到之前编码方面略有不同),但你永远都不知道。 Either way whenever he touches the back button it does not run the append code anymore. 无论何时,只要他触摸后退按钮,它就不再运行附加代码。 I am trying to get in contact with the trainer as some of the suggestions here, though they are good don't seem to work. 我试图与培训师联系,作为这里的一些建议,虽然它们很好似乎不起作用。 I will put the solution in here once I get in contact with the trainer. 一旦我与培训师联系,我会把解决方案放在这里。

The viewDidLoad method is called when your view controller's view finishes loading. 视图控制器的view完成加载时,将调用viewDidLoad方法。 The view will load when a view controller's view property is nil and something attempts to access it. 当视图控制器的view属性为nil且某些内容尝试访问它时, view将加载。

UIViewController *myVC = [[UIViewController alloc] init];
UIView *aView = myVC.view; // this loads myVC's view; viewDidLoad is called when it completes loading.

If the view has unloaded (usually due to memory limitations), it will be called when the view property is next accessed. 如果view已卸载(通常由于内存限制),则在下次访问view属性时将调用该view

Manipulation of data sets should generally not be done within view methods. 通常不应在视图方法内完成数据集的操作。 Consider moving this to the init of the view controller (or to a different "datasource" class). 考虑将其移动到视图控制器的init (或不同的“数据源”类)。

viewDidLoad is called whenever the view controller's view property is set. 只要设置了视图控制器的视图属性,就会调用viewDidLoad When does this happen? 这是什么时候发生的? It depends on how the view controller is contained: 这取决于视图控制器的包含方式:

UINavigationController - View Controller views are loaded as they are added to the navigation stack and "unloaded" (although the viewDidUnload method is deprecated) as they are removed. UINavigationController - 视图控制器视图在添加到导航堆栈时加载,并在删除时“卸载”(尽管不推荐使用viewDidUnload方法)。

UITabBarController - View Controller views are loaded as they are added to the tab bar regardless of whether they are on screen or not. UITabBarController - 视图控制器视图在添加到选项卡栏时加载,无论它们是否在屏幕上。 They stay loaded as you change from tab to tab. 当您从标签更改为标签时,它们会保持加载状态。

Depending on your needs and use case, you can create your own view controller container that does what you need. 根据您的需要和用例,您可以创建自己的视图控制器容器,以满足您的需要。 Checkout the Apple docs on the proper way to do this: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html 查看Apple文档以正确的方式执行此操作: https//developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

I suppose you are trying to do data initialisation in viewDidLoad. 我想你正在尝试在viewDidLoad中进行数据初始化。 If there is no other operation on placesArray before viewDidLoad, then instead of append, what about setting the placesArray directly: 如果在viewDidLoad之前没有对placesArray进行其他操作,那么可以直接设置placesArray而不是追加:

placesArray = ["name":"Taj Mahal", "lat":"27.175607", "lon":"78.042112"]

Then even if your view is unloaded for some reasons. 然后,即使您的视图由于某些原因被卸载。 Taj Mahal will still be added once only. 泰姬陵仍将只增加一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM