UIToolbar & UIProgressView & KVO

UIToolbar holds and shows a collection of UIBarButtonItem objects that the user can tap on. The way we're going to use UIToolbar is quite simple: all view controllers automatically come with a toolbarItems array that automatically gets read in when the view controller is active inside a UINavigationController.

UIProgressView is a colored bar that shows how far a task is through its work, sometimes called a "progress bar."

KVO: Key Value Observing: it effectively lets you say, "please tell me when the property X of object Y gets changed by anyone at any time."

Although WKWebView tells us how much of the page has loaded using its estimatedProgress property, the WKNavigationDelegate system doesn't tell us when this value has changed. So, we're going to ask iOS to tell us using a powerful technique called key-value observing, or KVO.

```webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)```

The addObserver() method takes four parameters: who the observer is (we're the observer, so we use self), what property we want to observe (we want the estimatedProgress property), which value we want (we want the value that was just set, so we want the new one), and a context value.

forKeyPath and context bear a little more explanation. forKeyPath isn't named forProperty because it's not just about entering a property name. You can actually specify a path: one property inside another, inside another, and so on. More advanced key paths can even add functionality, such as averaging all elements in an array!

context is easier: if you provide a unique value, that same context value gets sent back to you when you get your notification that the value has changed. This allows you to check the context to make sure it was your observer that was called. There are some corner cases where specifying (and checking) a context is required to avoid bugs, but you won't reach them during any of this series.

Warning: in more complex applications, all calls to addObserver() should be matched with a call to removeObserver() when you're finished observing – for example, when you're done with the view controller.

Once you have registered as an observer using KVO, you must implement a method called observeValueForKeyPath(). This tells you when an observed value has changed,

你可能感兴趣的:(UIToolbar & UIProgressView & KVO)