31
CS 646 Mobile Application Development Fall Semester, 2011 Doc 15 Nib files and Controllers Oct 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

CS 646 Mobile Application Development Fall Semester, 2011

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 2: CS 646 Mobile Application Development Fall Semester, 2011

Basic Setup

2

UIApplicationDelegate

View

Controller

Model

OS

Your Code

Tuesday, October 18, 11

Page 3: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 4: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 5: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 6: CS 646 Mobile Application Development Fall Semester, 2011

First Responder

6

GUI element that current receives most events first

Changes depending on which GUI has focus

Tuesday, October 18, 11

Page 7: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 8: CS 646 Mobile Application Development Fall Semester, 2011

FileOwner

8

Controller

Connected to nib file when loading nib

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

Tuesday, October 18, 11

Page 9: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 10: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 11: CS 646 Mobile Application Development Fall Semester, 2011

Connecting View to Controller

11

Preferred way

Hidden IBOutlet view in controller

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

Tuesday, October 18, 11

Page 12: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 13: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 14: CS 646 Mobile Application Development Fall Semester, 2011

The Action

14

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

Tuesday, October 18, 11

Page 15: CS 646 Mobile Application Development Fall Semester, 2011

Xcode 4.2, Outlets & Actions

15

Tuesday, October 18, 11

Page 16: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 17: CS 646 Mobile Application Development Fall Semester, 2011

MainWindow.xib

17

ContainsDelegateController to use

Controller as own nib fileWindow

Tuesday, October 18, 11

Page 18: CS 646 Mobile Application Development Fall Semester, 2011

Controller Occupies Entire Window

18

So controllers viewis all you see

Tuesday, October 18, 11

Page 19: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 20: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 21: CS 646 Mobile Application Development Fall Semester, 2011

window.rootViewController

21

root view controller provides the content view of the window

Changing root view controller changes the view

Tuesday, October 18, 11

Page 22: CS 646 Mobile Application Development Fall Semester, 2011

MainWindow.xib

22

Creates window

Gives app delegatewindowcontroller to use

Tuesday, October 18, 11

Page 23: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 24: CS 646 Mobile Application Development Fall Semester, 2011

24

How to Move to Different Screen

Tuesday, October 18, 11

Page 25: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 26: CS 646 Mobile Application Development Fall Semester, 2011

rootViewController

26

Changing root view controller changes the view displayed

Tuesday, October 18, 11

Page 27: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 28: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 29: CS 646 Mobile Application Development Fall Semester, 2011

Utility app and Modal Change

29

Tuesday, October 18, 11

Page 30: CS 646 Mobile Application Development Fall Semester, 2011

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

Page 31: CS 646 Mobile Application Development Fall Semester, 2011

Storyboarding

31

New in Xcode 4.2

Don't use xib files

Tuesday, October 18, 11