Lecture #15: Modal View Controller/Text/Animation/Timer

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

Lecture fifteen is named “15. Modal View Controller/Text/Animation/Timer (November 15, 2011)” and can be found at iTunes. Its slides are available at Stanford.

The theoretical part of this lecture provides an introduction to modal view controllers, text fields, animations and the usage of timers.

Modal View Controllers provide views which fill the complete screen and should only be used if it is necessary to block all other navigation or tab controllers. They can be setup in storyboard using modal segues or via code, e.g.:

AddressLookupViewController *alvc =
        [self.storyboard instantiateViewControllerWithIdentifier:@“AddressLookup”];
[self presentModalViewController:alvc 
                            // on screen

They are closes by dismissing them form the parents view controller:

- (void)dismissModalViewControllerAnimated:(BOOL)animated;

If needed a modal view controller can dismiss itself by calling its parent:

[[self presentingViewController] dismissModalViewController:YES];

A better way would be using delegation which can also be used to report back results from the modal view controller.

The animation of how the modal view controller appears on screen is set up using its property modalTransitionStyle. modalPresentationStyle can be used to setup its appearance for iPads.

Entering text in apps should be reduced to a minimum for the user experience. UITextFields are single line fields, UITextViews are multi line. The keyboard appears when they are selected or when the becomeFirstResponder message is sent to text fields. resignFirstResponder closes the keyboard.

Text from text fields can be obtained using

- (BOOL)textFieldShouldReturn:(UITextField *)sender;
- (void)textFieldDidEndEditing:(UITextField *)sender;

The appearance of the keyboard is controlled via several properties, e.g.:

@property UITextAutocapitalizationType autocapitalizationType;
@property UITextAutocorrectionType autocorrectionType;
@property UIReturnKeyType returnKeyType;
@property BOOL secureTextEntry;
@property UIKeyboardType keyboardType;

To make sure a text field is not covered by the keyboard, a view might need repositioning which can be handled using keyboard notifications, e.g.:

[[NSNotificationCenter defaultCenter] addObserver:self

Certain UIView properties can be animated using UIView class methods which are powered by the Core Animation framework:

+ (void)animateWithDuration:(NSTimeInterval)duration
                 animations:(void (^)(void))animations
                 completion:(void (^)(BOOL finished))completion;

Inside the animation block the properties are set to the values they should have at the end of the animation. While the animation on screen takes a certain time and/or starts with a delay the animated properties are set immediately to their end results.

Also transitions between views can be animated:

+ (void)transitionFromView:(UIView *)fromView
                    toView:(UIView *)toView
                completion:(void (^)(BOOL finished))completion;

+ (void)transitionWithView:(UIView *)view
                animations:(void (^)(void))animations
                completion:(void (^)(BOOL finished))completion;

NSTimer can be used to schedule a method in the main queue, e.g.:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)seconds

Due to the possibility of blocking the main tread because of getting called to often, or taking too long to process it should only be used for quick status updates.

The demo of this lecture shows how to create modal view controllers, to use text fields and animation. The complete code for this demo is available directly at Stanford and on github.


Flattr this!

Leave a Reply

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