05 communicating with controllers2.action method creates dest controller 3.[pass data to dest in...

69
Communicating with Controllers CS 442: Mobile App Development Michael Saelee <[email protected]>

Upload: others

Post on 18-Jun-2020

23 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Communicating with Controllers

CS 442: Mobile App DevelopmentMichael Saelee <[email protected]>

Page 2: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Agenda- Recap: target-action & outlets

- Delegation

- Controller-Controller communication

- Container controllers

Page 3: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

§Recap

Page 4: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Controllers act as intermediaries between the UI (views) and our application logic

Page 5: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Simple views either push events to controllers via target-action, or act as outlets for controllers (and can be used for I/O)

Page 6: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

A complex view, however,

1. May have many types of actions it needs to notify a controller about

2. May need to pull data from a controller according to some internal logic

Page 7: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Controller should expose a well-defined API for interacting with a complex view

Page 8: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Well defined API → formal protocol

Page 9: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

§Delegation

Page 10: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Clean separation of responsibilities:

- delegator is the source of event/information

- delegate (aka delegatee) is responsible for acting on the event/information

Page 11: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Important: delegator and delegate are typically independent objects/actors

- weak connection!

Page 12: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Also: may have multiple delegators sending events to a single delegate

- use source object in API to differentiate between delegators

Page 13: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

E.g., TableView ↔ Datasource / Delegate

Page 14: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

@protocol UITableViewDelegate- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;@end

@protocol UITableViewDataSource- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;@end

Page 15: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Separation of responsibilities!

- TableView manages views

- including caching of cells

- Controller informs model of events

- insertion/deletion requests, etc.

- propagates changes to TableView

Page 16: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Demo:

TableView cell caching (instruments)

Page 17: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

so far, single controller and 1+ views

= a single scene

Page 18: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

but most interesting apps require 1+ scenes

Page 19: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

§Controller-Controller communication

Page 20: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

�& + � �!)*+ +.' *��&�)!'*� + � &�-!��+!'& �'&+)'$$�) ()'-!��* � �),�!�$ (�)+ '� 0',) ��*!� !&+�)���� �&� *+�0*!& ($��� ,&+!$ + � �(($!��+!'& �/!+*� �'.�-�)� + � $�*+ *��&�)!' )��$��+* � %')� +�%(')�)0 ,*� �') &�-!��+!'&�'&+)'$$�)*� !& . !� ��*� + � ()'��** �') ,*!&� + � &�-!��+!'& �'&+)'$$�) !* !��&+!��$ +' + � ()'��** �') '+ �)%'��$ �&� �,*+'% -!�. �'&+)'$$�)*� � � '&$0 �!���)�&�� !* + �+ + � &�-!��+!'& �'&+)'$$�) ()'-!��* %')� + �&� *!&�$� *�)��&4* .')+ '� �'&+�&+� �$+ ',� + � �'$$'.!&� *��+!'&* �'�,* '& '. 0', �)��+� + � %')�(�)%�&�&+ +0(�* '� &�-!��+!'& !&+�)����� %'*+ '� + � �,*+'%!1�+!'& *+�(* �&� ��&�)�$ !&�')%�+!'& �(($0 +'�$$ &�-!��+!'& �'&+)'$$�)*� )���)�$�** '� '. 0', !&+�&� +' ,*� + �%�

��"�����') � *(��!�!� �/�%($� '� '. +' ()�*�&+ � &�-!��+!'& �'&+)'$$�) %'��$$0� *�� 2 !*($�0!&� � ��-!��+!'&�'&+)'$$�) �'��$$03��(���������

��!&!&� + � �,*+'% �!�. �'&+)'$$�)* �') � ��-!��+!'& �&+�)����

�&� '� + � #�0 + !&�* 0', %,*+ �' +' !%($�%�&+ � &�-!��+!'& !&+�)���� !* ���!�� . �+ ��+� 0', ($�& +'()�*�&+ �+ ��� *+���� �-�)0 &�-!��+!'& !&+�)���� �* �+ $��*+ '&� $�-�$ '� ��+� + �+ )�()�*�&+* + � )''+ $�-�$'� ��+�� � !* !* + � *+�)+!&� ('!&+ '� 0',) !&+�)����� �') �/�%($�� + � � '+'* �(($!��+!'& �!*($�0* + � $!*+ '��-�!$��$� ( '+' �$�,%* �+ + � )''+ $�-�$ '� !+* ��+� !�)�)� 0� ��$��+!&� � ( '+' �$�,% + �& �!*($�0* + �( '+'* !& + �+ �$�,% �&� *�$��+!&� � ( '+' * '.* � $�)��) -�)*!'& '� + � ( '+'�

�') ��� $�-�$ '� 0',) ��+� !�)�)� 0� 0', %,*+ ()'-!�� � �,*+'% -!�. �'&+)'$$�) '�"��+ +' %�&��� �&�()�*�&+ + � ��+� �+ + �+ $�-�$� �� + � ()�*�&+�+!'& �+ %,$+!($� $�-�$* !* �**�&+!�$$0 + � *�%�� 0', ��& )�,*� + �*�%� -!�. �'&+)'$$�) �$�** !� 0', .�&+� �'.�-�)� �+ ),&+!%�� 0', %,*+ *+!$$ �)��+� *�(�)�+� !&*+�&��* '� + �+�$�** �&� �'&�!�,)� ��� '&� +' %�&��� !+* '.& *�+ '� ��+�� �') �/�%($�� + � � '+'* �(($!��+!'& �* + )���!*+!&�+ ()�*�&+�+!'& +0(�*� *' !+ .',$� &��� +' �� + )�� *�(�)�+� -!�. �'&+)'$$�) '�"��+*� *,� �* + � '&�** '.& !& �!�,)� ��

���# � ��� ��!&!&� -!�. �'&+)'$$�)* �') ��� $�-�$ '� ��+�

Album list controller Photo album controller Photo controller

�)��+!&� � ��-!��+!'& �&+�)���� ����������� %���&������������������������"!���!� $���

������ �

��-!��+!'& �'&+)'$$�)*

Page 21: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

�& + � �!)*+ +.' *��&�)!'*� + � &�-!��+!'& �'&+)'$$�) ()'-!��* � �),�!�$ (�)+ '� 0',) ��*!� !&+�)���� �&� *+�0*!& ($��� ,&+!$ + � �(($!��+!'& �/!+*� �'.�-�)� + � $�*+ *��&�)!' )��$��+* � %')� +�%(')�)0 ,*� �') &�-!��+!'&�'&+)'$$�)*� !& . !� ��*� + � ()'��** �') ,*!&� + � &�-!��+!'& �'&+)'$$�) !* !��&+!��$ +' + � ()'��** �') '+ �)%'��$ �&� �,*+'% -!�. �'&+)'$$�)*� � � '&$0 �!���)�&�� !* + �+ + � &�-!��+!'& �'&+)'$$�) ()'-!��* %')� + �&� *!&�$� *�)��&4* .')+ '� �'&+�&+� �$+ ',� + � �'$$'.!&� *��+!'&* �'�,* '& '. 0', �)��+� + � %')�(�)%�&�&+ +0(�* '� &�-!��+!'& !&+�)����� %'*+ '� + � �,*+'%!1�+!'& *+�(* �&� ��&�)�$ !&�')%�+!'& �(($0 +'�$$ &�-!��+!'& �'&+)'$$�)*� )���)�$�** '� '. 0', !&+�&� +' ,*� + �%�

��"�����') � *(��!�!� �/�%($� '� '. +' ()�*�&+ � &�-!��+!'& �'&+)'$$�) %'��$$0� *�� 2 !*($�0!&� � ��-!��+!'&�'&+)'$$�) �'��$$03��(���������

��!&!&� + � �,*+'% �!�. �'&+)'$$�)* �') � ��-!��+!'& �&+�)����

�&� '� + � #�0 + !&�* 0', %,*+ �' +' !%($�%�&+ � &�-!��+!'& !&+�)���� !* ���!�� . �+ ��+� 0', ($�& +'()�*�&+ �+ ��� *+���� �-�)0 &�-!��+!'& !&+�)���� �* �+ $��*+ '&� $�-�$ '� ��+� + �+ )�()�*�&+* + � )''+ $�-�$'� ��+�� � !* !* + � *+�)+!&� ('!&+ '� 0',) !&+�)����� �') �/�%($�� + � � '+'* �(($!��+!'& �!*($�0* + � $!*+ '��-�!$��$� ( '+' �$�,%* �+ + � )''+ $�-�$ '� !+* ��+� !�)�)� 0� ��$��+!&� � ( '+' �$�,% + �& �!*($�0* + �( '+'* !& + �+ �$�,% �&� *�$��+!&� � ( '+' * '.* � $�)��) -�)*!'& '� + � ( '+'�

�') ��� $�-�$ '� 0',) ��+� !�)�)� 0� 0', %,*+ ()'-!�� � �,*+'% -!�. �'&+)'$$�) '�"��+ +' %�&��� �&�()�*�&+ + � ��+� �+ + �+ $�-�$� �� + � ()�*�&+�+!'& �+ %,$+!($� $�-�$* !* �**�&+!�$$0 + � *�%�� 0', ��& )�,*� + �*�%� -!�. �'&+)'$$�) �$�** !� 0', .�&+� �'.�-�)� �+ ),&+!%�� 0', %,*+ *+!$$ �)��+� *�(�)�+� !&*+�&��* '� + �+�$�** �&� �'&�!�,)� ��� '&� +' %�&��� !+* '.& *�+ '� ��+�� �') �/�%($�� + � � '+'* �(($!��+!'& �* + )���!*+!&�+ ()�*�&+�+!'& +0(�*� *' !+ .',$� &��� +' �� + )�� *�(�)�+� -!�. �'&+)'$$�) '�"��+*� *,� �* + � '&�** '.& !& �!�,)� ��

���# � ��� ��!&!&� -!�. �'&+)'$$�)* �') ��� $�-�$ '� ��+�

Album list controller Photo album controller Photo controller

�)��+!&� � ��-!��+!'& �&+�)���� ����������� %���&������������������������"!���!� $���

������ �

��-!��+!'& �'&+)'$$�)*

Page 22: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

@implementation  AppDelegate-­‐  (BOOL)application:(UIApplication  *)application                  didFinishLaunchingWithOptions:(NSDictionary  *)launchOptions{        ...        self.viewController1  =  [[FirstViewController  alloc]                                                          initWithNibName:@"FirstViewController"  bundle:nil];        self.viewController2  =  [[SecondController  alloc]                                                          initWithNibName:@"SecondController"  bundle:nil];        self.window.rootViewController  =  viewController1;        ...}

-­‐  (void)toggleViewControllers  {        if  (self.window.rootViewController  ==  viewController1)                self.window.rootViewController  =  viewController2;        else                self.window.rootViewController  =  viewController1;}...@end

@implementation  FirstViewController-­‐  (IBAction)switchButtonPressed:(id)sender  {        [((AppDelegate  *)[UIApplication  sharedApplication].delegate)  toggleViewControllers];}@end

Page 23: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

NOOO!!! iOS spaghetti code!

Page 24: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

need a better mechanic for managing relationships between view controllers!

ideally, decouple VCs as much as possible

Page 25: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

specific goals:

- only presenter ought know about presentee

- presentee shouldn’t know too much about its presenter or other ancestors

Page 26: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

maximize reusability & flexibility of application components

Page 27: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

simplest relationship: parent-child

aka “modular” relationship; e.g., desktop dialog window

Page 28: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

@implementation  MainViewController-­‐  (IBAction)addButtonPressed:(id)sender  {        EditingViewController  *vc  =  [[EditingViewController  alloc]  ...];        vc.delegate  =  self;        [self  presentViewController:vc  animated:YES  completion:nil];}@end

Page 29: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

when/how to dismiss?

@implementation  EditingViewController-­‐  (void)textFieldDidEndEditing:(UITextField  *)textField  {        [self.presentingViewController  dismissViewControllerAnimated:YES  completion:nil];}@end

Page 30: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

but not (always) logically sound …

parent should dismiss child!

@implementation  EditingViewController-­‐  (void)textFieldDidEndEditing:(UITextField  *)textField  {        MainViewController  *parent  =  (MainViewController  *)self.presentingViewController;        [parent  childControllerFinishedWithResult:textField.text];}@end

Page 31: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

much better solution: develop an API for delegation!

Page 32: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

@protocol  EditingViewControllerDelegate  <NSObject>-­‐  (void)editingViewController:(EditingViewController  *)viewController                                      textEntered:(NSString  *)str;@end

@interface  EditingViewController  :  UIViewController@property(weak)  id<EditingViewControllerDelegate>  delegate;@end

@implementation  EditingViewController-­‐  (void)textFieldDidEndEditing:(UITextField  *)textField  {        [delegate  editingViewController:self  textEntered:textField.text];}@end

-­‐  (void)editingViewController:(EditingViewController  *)viewController                                      textEntered:(NSString  *)str  {        [self  dismissViewControllerAnimated:YES  completion:nil];}@end

@interface  MainViewController  :  UIViewController  <EditingViewControllerDelegate>@end

@implementation  MainViewController-­‐  (IBAction)addButtonPressed:(id)sender  {        EditingViewController  *vc  =  [[EditingViewController  alloc]  ...];        vc.delegate  =  self;        [self  presentViewController:vc  animated:YES  completion:nil];}

Page 33: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

lot of typing, but much cleaner!

Page 34: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Similar types of delegation are used for built-in iOS controllers; e.g.,

- mail composer

- address book

- photo picker / camera

- Twitter

Page 35: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Demo:

DoesItAll

Page 36: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

A storyboard driven alternative: segues

Page 37: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Segues represent transitions between scenes (controllers)

- visual elements in storyboards

- can be accessed as objects in code(UIStoryboardSegue)

Page 38: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Page 39: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

A segue tracks:

1. Source controller

2. Destination controller

3. Segue “name” (unique in storyboard)

Page 40: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

When a segue is triggered:

- the destination controller is created

- prepareForSegue is called in source

- an [animated] transition from source to destination controller takes place

Page 41: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Segues seem magical, but really just add another layer of abstraction on top of what we already have

Page 42: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Controller transition without segue:

1. Event (e.g., button tap) triggers action

2. Action method creates dest controller

3. [Pass data to dest in action method]

4. Use presentViewController to activate dest controller

Page 43: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Controller transition with segue:

1. Event triggers creation of segue object

2. Segue populates its src/dest controllers

3. prepareForSegue called in src controller

4. [Pass data to dest in prepareForSegue]

Page 44: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Controller transition with segue:

5. Segue object’s perform is invoked

6. [perform method animates transition]

7. perform calls presentViewController to complete transition

Page 45: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

If we use built-in segues, most of this is automatic (i.e., invisible)

Page 46: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Can do away (sometimes) with cumbersome & wordy delegate mechanism

Page 47: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

When a segue is triggered:

- the destination controller is created

- prepareForSegue is called in source

- good place to send data to destination

(slight problem)

Page 48: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

(sometimes we want to return to an existing controller/scene)

Page 49: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

“Unwind” segues let us return to an unwind action in a previous controller

Page 50: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Conveniently, segue stores and retains source controller — can retrieve data in destination before source goes away

/* Sample unwind segue action */- (IBAction)unwindSegue:(UIStoryboardSegue *)segue{ ViewController *srcController = segue.sourceViewController;

/* retrieve data from srcController */

/* note: manually dismissing srcController isn’t done here! */}

Page 51: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Demo:

Segues

Page 52: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

§Container controllers

Page 53: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

so far, we can create a modal VC relationship

… but that’s not always enough to build/describe a complex app

Page 54: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Page 55: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Page 56: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

other typical VC relationships:

- hierarchical / drill-down

- sibling / parallel

— “navigation”

— “tabbed”

Page 57: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

in both cases, there is some overarching context for the related VCs

Page 58: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

navigation bar

Page 59: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

tab bar

Page 60: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

UINavigationController & UITabBarController = container view controllers

Page 61: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

container view controllers track and manage transitions between other view controllers

- implement custom transitions

- simplify controller management & communication

Page 62: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

e.g., UINavigationController

Page 63: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

views managed by different VCs on screensimultaneously (previously a no-no)

Page 64: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

NavController manages a stack of related view controllers

Page 65: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

NavController also exposes an API formanaging the navigation & tool bars

Page 66: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Page 67: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Page 68: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

UINavigationControllerCreating  Navigation  Controllers

–  initWithRootViewController:Accessing  Items  on  the  Navigation  Stack

   topViewController    property    visibleViewController    property    viewControllers    property–  setViewControllers:animated:

Pushing  and  Popping  Stack  Items–  pushViewController:animated:–  popViewControllerAnimated:–  popToRootViewControllerAnimated:–  popToViewController:animated:

UIViewControllerGetting  the  Navigation  controller

   navigationController    propertyConfiguring  a  Navigation  Interface

   navigationItem    property    hidesBottomBarWhenPushed    property–  setToolbarItems:animated    toolbarItems    property

UINavigationItemGetting  and  Setting  Properties

   title    property    backBarButtonItem    property    hidesBackButton    property

Customizing  Views    leftBarButtonItem    property    rightBarButtonItem    property

Page 69: 05 Communicating with Controllers2.Action method creates dest controller 3.[Pass data to dest in action method] 4.Use presentViewController to activate dest controller Computer Science

Computer ScienceScience

Demo (ad hoc):

NavDemo