Lecture #8: Controller Lifecycle & Image/Scroll/WebViews

Please note, this blog entry is from a previous course. You might want to check out the current one.

Lecture eight is named “8. Controller Lifecycle & Image/Scroll/WebViews (October 20, 2011)” and can be found at iTunes. Its slides are available at Stanford.

It starts with a theoretical part about the View Controller Lifecycle, the Image, the Web and the Scroll View.

First in the life cycle of a view controller it gets created via a segue or storyboard’s instantiateViewControllerWithIdentifer:. Quite early in the life cycle awakeFromNib is called, but has no outlets connected yet.

As soon as the controller is fully instantiated and its outlets are hooked up viewDidLoad: is called. Thus this is a good place to put setup code. However the size of controller is not set yet, but in viewWillAppear: which is called just before the view appears on screen.

viewWillDisappear: is called just before a view disappears from screen where code to save a state can be put. However, states should be saved as soon as they change, to do not loose important information.

In addition viewDidAppear: and viewDidDisappear: as soon as a view appeared on or disappeared from screen.

All of these functions should called their super methods when overwritten.

view{Will,Did}LayoutSubviews are called when frames change, e.g. when rotating a device, where rotation actually has its own subset of life-cycle methods willRotateToInterfaceOrientation:, willAnimateRotationToInterfaceOrientation: and didRotateFromInterfaceOrientation: and property interfaceOrientation.

viewDidUnload is called in low-memory situations when a view is thrown out of the heap. Best practice in this case is to set one’s view to nil.

An UIImageView is a subclass of UIView to display UIImages on screen with special features like image highlighting or animation of a sequence of images.

An UIWebView is a full internet browser in a UIView built on WebKit which supports Javascript limited to 5 seconds and/or 10 MBytes of memory allocation. The property scalesPagesToFit allows to adjust a page to the width of the view. If set to NO, the page will be displayed in its natural size and the user will not be able to change it.

A page loads via loadRequest:, loadHTMLString:, or loadData:, where the most common is the fist one using NSURLRequest, which is an NSUrl with additional information about its NSURLRequestCachePolicy.

An UIScrollView shows a small content area of a bigger view with all features needed to provide scrolling and zoomings. To show the user that the view is scrollable flashScrollIndicators can be called, or an uneven number of should be shown. Zooming needs maximum and minimum zoom scales set as well as a delegate.

The second part shows first a demo how to add a web page view to psychologist (code available on github) and a second demo showing how to implement a scroll view with code available directly at Stanford or at github.


Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *