Difficulty: Beginner | Easy | Normal | Challenging
难度:初学者| 容易 | 普通| 具有挑战性的
Some knowledge of how to build a single view application
关于如何构建单视图应用程序的一些知识
There is always much talk and thought about “what goes where” when thinking about and designing new iOS apps. You might well be familiar with the Application Life Cycle, but were you aware of the view controller’s similar journey?
在思考和设计新的iOS应用程序时,总是有很多关于“什么去哪里”的讨论和思考。 您可能非常熟悉Application Life Cycle ,但是您是否知道视图控制器的类似过程?
Even if you are, I hope this article will help you out in understanding this essential part of UIKit.
即使您愿意,我也希望本文能帮助您理解UIKit的这一基本部分。
A new instance of a UIViewController can be set up by simply creating a new Single View Project from Xcode. This quickly sets up the following:
只需通过Xcode创建新的Single View Project,就可以设置UIViewController新实例。 快速设置以下内容:
Click for Gist 单击要点Which reveals that there are important thing to note. The ViewController is actually a subclass of UIViewController, which means that we have access to various methods which we can override to use.
这表明需要注意一些重要的事情。 ViewController实际上是UIViewController的子类,这意味着我们可以访问可以重写以使用的各种方法。
The first of these is viewDidLoad() which most developers are familiar with, and I will go into detail about later in this article.
其中第一个是大多数开发人员都熟悉的viewDidLoad() ,我将在本文后面详细介绍。
We are already given the like super.viewDidLoad() which is great, since we should be calling super for our overriden function.
我们已经被赋予了类似super.viewDidLoad()功能,因为我们应该为我们的重写函数调用super。
If you can get this far you can already do great things like change the background of the view controller with a line like self.view.backgroundColor = .blue that is placed right in the viewDidLoad().
如果可以做到这一点,那么您已经可以做很多很棒的事情,例如使用诸如self.view.backgroundColor = .blue这样的一行来更改视图控制器的背景,该行直接位于viewDidLoad() 。
This is called after the view is loaded in to memory, and is commonly thought of as the first method that will be called.
在将视图加载到内存后调用此方法,通常认为这是将被调用的第一个方法。
You should not be doing layout in this method — it simply is too soon. You cannot assume that the bounds and frame that are given to you in viewDidLoad() are set and final (they might be correct, they might not be).
你不应该 用这种方法进行布局-太早了。 您不能假定在viewDidLoad()中给定的边界和框架是最终的(它们可能是正确的,但可能不是)。
It is, of course, fine to configure views that will be calculated later. For example adding a subview that is placed with autolayout is absolutely fine, and here is an example of just that:
当然,最好配置稍后将要计算的视图。 例如,添加一个使用自动布局放置的子视图绝对可以,下面是一个示例:
Click for Gist 单击要点This gives you the following rather dashing view.
这为您提供了以下相当破折的视图。
Nice!
真好!
This is called just before the view controller is added to the view hierarchy and shown to the user. This means that the frame and bounds are all set, but we need to remember that these methods are called each and every time that the view controller is presented to he screen.
在将视图控制器添加到视图层次结构并显示给用户之前,将调用此方法。 这意味着框架和边界都已设置,但是我们需要记住,每次将视图控制器呈现给屏幕时,都会调用这些方法。
Similarly to viewDidLoad(_:) when overriding we should call super (that is super.viewWillAppear(_:))
与覆盖时的viewDidLoad(_:)类似,我们应该调用super(即super.viewWillAppear(_:) )
Also known as the place to start animations, this is called when the user can see it. It’s not usually a good idea to flash things onto the screen at this point, as the user will see it!
也称为开始动画的地方,当用户可以看到它时调用该地方。 此时,将内容闪烁到屏幕上通常不是一个好主意,因为用户会看到它!
As before, we should call super (that is super.viewDidAppear(_:))
和以前一样,我们应该调用super(即super.viewDidAppear(_:) )
The bounds are known and are guaranteed to be correct (as is the frame)! This can be called many times during the life of the view controller.
边界是已知的,并且保证是正确的(框架也是如此)! 在视图控制器的生命周期中,可以多次调用此方法。
This is extremely important when the orientation of the device is changed. This can be called many times during the life of the view controller. Imagine we add a button at the bottom of the screen using the storyboard.
当改变设备的方向时,这是非常重要的。 在视图控制器的生命周期中,可以多次调用此方法。 想象一下,我们使用情节提要在屏幕底部添加了一个按钮。
Now if we rotate the phone…where has the UIButton gone?
现在,如果我们旋转手机…… UIButton哪里去了?
We can use viewDidLayoutSubviews() to reposition the button. We need to be aware that the function is called when the view is initially setup (that is, on the first instantiation of the view controller).
我们可以使用viewDidLayoutSubviews()重新定位按钮。 我们需要注意的是,在最初设置视图时(即,在视图控制器的第一个实例上)会调用该函数。
To carefully position the button we can use the frame of the view.
要仔细定位按钮,我们可以使用视图框架。
Click for Gist 单击要点Where this function is simply placed within the view controller.
该功能仅放置在视图控制器中的位置。
Called when the view is about to disappear from the user’s view. A good place to save user data and cancel network tasks.
当视图即将从用户视图中消失时调用。 保存用户数据和取消网络任务的好地方。
As before, we should call super (that is super.viewWillDisapper(_:))
和以前一样,我们应该调用super(即super.viewWillDisapper(_:) )
Called when the view controller has gone from the user’s view. The view has already disappeared, something like stopping music playing at this time would be a good choice.
当视图控制器从用户视图移开时调用。 视图已经消失,此时停止播放音乐之类的东西是一个不错的选择。
As before, we should call super (that is super.viewDidDisapper(_:))
和以前一样,我们应该调用super(即super.viewDidDisapper(_:) )
The functions were called in the following order:
这些函数按以下顺序调用:
viewDidLoad()viewWillAppear(_:)viewWillLayoutSubviews()viewDidLayoutSubviews()viewDidAppear(_:)
viewDidLoad() viewWillAppear(_:) viewWillLayoutSubviews() viewDidLayoutSubviews()viewDidAppear(_:)
when the following code is run. You can check this out for yourself by either placing breakpoints in each method, or print logging (there’s a print(“will”) and print(“did”) there already). Here is the code (there is a link to the Gist just below the code if you want to copy — paste), which fully shows the black cube example shown above in this tutorial.
运行以下代码时。 您可以通过在每种方法中放置断点或打印日志记录(那里已经有一个print(“will”)和print(“did”)亲自检查一下。 这是代码(如果要复制-粘贴,则在代码下方有一个指向Gist的链接),其中完全显示了本教程上面显示的黑色立方体示例。
Click for Gist 单击要点The little things in our projects make huge differences in both the user experience and how easy it is to maintain and add to our projects. Apple often refer to viewDidLoad(), viewDidAppear(), viewDidDisappear(), viewWillAppear(), viewWillDisappear() but much less is made of viewDidLayoutSubviews() yet to overcome layout problems, let us just say you will be using this method.
我们项目中的小事在用户体验以及维护和添加到我们项目中的难度方面都产生了巨大差异。 Apple经常引用viewDidLoad() , viewDidAppear() , viewDidDisappear() , viewWillAppear() , viewWillDisappear()但是要克服布局问题,目前很少使用viewDidLayoutSubviews() ,让我们说您将使用此方法。
Any questions? Get in touch with me on Twitter or follow me here, it all helps these tutorials get made.
任何问题? 在Twitter上与我联系或在此处关注我,这些都有助于制作这些教程。
This guide should not only help you to use these functions, but even use them correctly. I hope this helps you.
本指南不仅应帮助您使用这些功能,甚至应正确使用它们。 我希望这可以帮助你。
Read Apple’s documentation for UIViewController
阅读Apple的UIViewController文档
翻译自: https://medium.com/@stevenpcurtis.sc/the-uiviewcontroller-lifecycle-b964cf18256b
相关资源:四史答题软件安装包exe