CS 646 Mobile Application Development Fall Semester, 2011

Preview:

Citation preview

CS 646 Mobile Application DevelopmentFall Semester, 2011

Doc 15 Nib files and ControllersOct 18, 2011

Copyright ©, All rights reserved. 2011 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/openpub/) license defines the copyright on this document.

Tuesday, October 18, 11

Basic Setup

2

UIApplicationDelegate

View

Controller

Model

OS

Your Code

Tuesday, October 18, 11

Nib files

3

nib - NeXT Interface Builderxib - Xml Interface Builder

Interface Builder started in 1988One of first commercial GUI builderDoes not generate GUI codeGenerates description of GUI codeDescription stored in nib file

only nib files until Interface builder 3.0

xibxml version of nib fileUsed during developmentConverted into nib file when deploying application

Documentation refers to xib and nib as nib file

Tuesday, October 18, 11

Freeze Dried

4

GUI elements are archived into nib file

Process also calledSerializationMarshalling

nib files are said to be freeze dried

Tuesday, October 18, 11

nib file contents

5

PlaceholdersNot known until runtimeFile's owenerFirst ResponderCan add more

ObjectsMainly views for displayDelegatesControllersImagesSound

Things are added by interface builderSerialized into nib file

Tuesday, October 18, 11

First Responder

6

GUI element that current receives most events first

Changes depending on which GUI has focus

Tuesday, October 18, 11

7

Responder Chain

Event is sent to view it occurs in

If it does not handle event it is passed on tosuper view (or controller)

Views and Controllers are responders

First responder is first thing in responder chain

Tuesday, October 18, 11

FileOwner

8

Controller

Connected to nib file when loading nib

[[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil]

Tuesday, October 18, 11

Loading Nib File

9

1. Load raw data nib file into memory

2. Unarchives objectsAll objects conforming to NSCoder protocol initialized with initWithCoder:

Includes subclasses ofUIViewUIViewController

3. Reestablish IBOutlets & IBActionsCalls setValue:forKey: to create IBOutlet connection

Calls addTarget:action:forControlEvents: to create IBAction connection

Tuesday, October 18, 11

Loading Nib File

10

4. Sends awakeFromNib to GUI elementsNot sent to placeholder objects

5. Displays any window whose "Visible at launch time" attribute was enabled in nib

Tuesday, October 18, 11

Connecting View to Controller

11

Preferred way

Hidden IBOutlet view in controller

[[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil]

Tuesday, October 18, 11

Controller displays its View

12

nib files contain a view

nib files are connected to a controller

A view from nib file is set as controller's view

Active controller displays it view

Tuesday, October 18, 11

Simple Example - One Nib, Two Views

13

@interface ViewController : UIViewController

- (IBAction)goRed;

@property (retain, nonatomic) IBOutlet UIView *red;

@end

@interface UIViewController : UIResponder

- (IBAction) view;

@end

Tuesday, October 18, 11

The Action

14

- (IBAction)goRed { self.view = self.red;}

Tuesday, October 18, 11

Xcode 4.2, Outlets & Actions

15

Tuesday, October 18, 11

Avoid large nib files

16

nib file contents are read into memory and processed before displaying view

Only add elements needed

Previous example did not need to display two view at same time

So place second view in separate nib file

Tuesday, October 18, 11

MainWindow.xib

17

ContainsDelegateController to use

Controller as own nib fileWindow

Tuesday, October 18, 11

Controller Occupies Entire Window

18

So controllers viewis all you see

Tuesday, October 18, 11

App Delegate has two Outlets

19

@class SwitchViewViewController;

@interface SwitchViewAppDelegate : NSObject <UIApplicationDelegate> {

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet SwitchViewViewController *viewController;

@end

Tuesday, October 18, 11

Putting our Controller in Charge

20

@implementation SwitchViewAppDelegate

@synthesize window=_window;

@synthesize viewController=_viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES;}

Tuesday, October 18, 11

window.rootViewController

21

root view controller provides the content view of the window

Changing root view controller changes the view

Tuesday, October 18, 11

MainWindow.xib

22

Creates window

Gives app delegatewindowcontroller to use

Tuesday, October 18, 11

Xcode 4.2 and MainWindow.xib

23

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainViewController"

bundle:nil] autorelease]; self.window.rootViewController = self.mainViewController; [self.window makeKeyAndVisible]; return YES;}

Does not use MainWindow.xib

Just create UIWindow in code

Tuesday, October 18, 11

24

How to Move to Different Screen

Tuesday, October 18, 11

Ways of changing to different Screen

25

root view controller

Controllers designed to show different viewsTab view controllerNavigation controllerPage controllerModal change

Change subviews

Story Boarding (Xcode 4.2)

Tuesday, October 18, 11

rootViewController

26

Changing root view controller changes the view displayed

Tuesday, October 18, 11

Controllers designed to change Views

27

Tab view controllerEach tab has own controllerSelecting tab shows view of tab's controller

Navigation controller Maintains stack of controllersPushing controller on stack displays that controller's view

Tuesday, October 18, 11

But how does that work?

28

View can contain subviews

Can change the subview

[blueViewController.view removeFromSuperview];[self.view insertSubview:yellowViewController.view atIndex:0];

Tuesday, October 18, 11

Utility app and Modal Change

29

Tuesday, October 18, 11

Utility app and Modal Change

30

MyViewController *controller = [[[MyFlipsideViewController alloc] initWithNibName:@"MyViewController" bundle:nil] autorelease];

controller.delegate = self;controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

[self presentModalViewController:controller animated:YES];

Tuesday, October 18, 11

Storyboarding

31

New in Xcode 4.2

Don't use xib files

Tuesday, October 18, 11

Recommended