When to access subview properties in UIViewController Lifecycle?

Context of Problem:

I am trying to learn how to use the storyboard in Xcode 6 by placing three UIViews in the storyboard's topmost viewcontroller. One of the UIView's is blue, one is red, and one is yellow. I am trying to see if I can alter the UIView's programmatically by changing the background color of one of the UIViews. By Command + clicking the UIViewController class and looking at the appropriate functions to override, I've determined "viewDidAppear" is the final function to be called by the UIViewController in its setup code.

Problem:

Here is my override of the function:

override func viewDidAppear(animated: Bool) {
    self.bottomView!.backgroundColor = UIColor.blackColor()
    super.viewDidAppear(animated)
}

However, when I run this code the the screen with the three UIViews appears for a split second, with NO black-colored UIView, and then proceeds to crash with a

"fatal error: unexpectedly found nil while unwrapping an Optional value"

Question:

What function do I have to override in order to be able to programmatically change the properties of the UIView? What is the best-practices method of doing so?

EDIT:

I am using Storyboard so my three UIView's are declared in the beginning of my UIViewController class as such:

@IBOutlet var topLeftView: UIView?
@IBOutlet var topRightView: UIView?
@IBOutlet var bottomView: UIView?

It seems that none of the views are actually initialized when viewDidLoad gets called, because the result of the following line of code

println("(bottomView?)")

is "nil". How come this isn't getting initialized?


This is best done in viewDidLoad . viewDidAppear is called when the view is added into the view hierarchy, but it was not yet initiated. viewDidLoad is called when the view is allocated into memory. According to the docs for viewDidLoad :

You usually override this method to perform additional initialization on views that were loaded from nib files

You should override viewDidLoad and modify your UIView 's in there.

链接地址: http://www.djcxy.com/p/15382.html

上一篇: 在子视图中添加UIViewController

下一篇: 何时访问UIViewController生命周期中的子视图属性?