CS4521:   Mobile and Topics in Web Programming

iOS Presenting and Dismissing View Controllers from Other ViewControllers

 

How you present different views to user. Can be triggered by user events or other occurrences (timing, etc).

 

General Idea: View Controller 1 -> presents -> View Controller 2

 

 

NOTE: A View (Interface) is associated with a View Controller.

 

 

A motivating Example: Image Application ->Sending Image->People Picker

 

 

 

 

Presenting a View Controller (and associated View) through iOS Segues

the concept of how one View can present another View in iOS

 

Can setup Graphically from StoryBoard Interface Editor:
       >>here is example with multiple ViewControllers which are connected through iOS Segues which present other
            View Controllers

 

 

 

 

What is an iOS Segue

  • Segues Automatically Instantiate the Destination View Controller

  • represents a triggered transition that brings a new view controller into your app’s user interface.

 

iOS Segue Contains

  • object caused the segue to be triggered, known as the sender

  • Source view controller that starts the segue

  • Destination view controller to be instantiated

  • Kind of transition that should be used to bring the destination view controller onscreen

  • Optional identifier string that identifies that specific segue in the storyboard

 

 

 

 

HOW TO create iOS Segue Graphically

In StoryBoard Builder ---Drag from Widget in View 1 that triggers "showing" View 2 to View 2

 

iOS Segue Type: Now you get a choice of HOW to show the 2nd View

 

 

How to creagte iOS Segue in Program/Code

In the Source ViewController add the code here to present 2nd View Controller (the class is called SecondViewController)

Instantiating another view controller inside the same storyboard

NOTE: the method below can be triggered as event handling code on a widget or simply whenever you want to call it in your code.

- (IBAction)presentSpecialViewController:(id)sender { 
    UIStoryboard *storyboard = self.storyboard; 
    SecondViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"]; 
  
    // Configure the new view controller here. 
  
    [self presentViewController:svc animated:YES completion:nil];
} 

 

Type of iOS Segues

iOS Segues

Name

Interface Builder Symbol

Description

Show

../art/SB_H_segue_push_2x.png

Present the content in the detail or master area depending on the content of the screen. If the app is displaying a master and detail view, the content is pushed onto the detail area. If the app is only displaying the master or the detail, the content is pushed on top of the current view controller stack.

Show Detail

../art/SB_H_segue_push_2x.png

Present the content in the detail area. If the app is displaying a master and detail view, the new content replaces the current detail. If the app is only displaying the master or the detail, the content replaces the top of the current view controller stack.

Present Modally

../art/SB_H_segue_modal_2x.png

Present the content modally. There are options to choose a presentation style (UIModalPresentationStyle) and a transition style (UIModalTransitionStyle).

Present as Popover

../art/SB_H_segue_popover_2x.png

Present the content as a popover anchored to an existing view. There is an option to specify the possible directions of the arrow shown on one edge of the popover view (UIPopoverArrowDirection). There is also an option to specify the anchor view.

Custom

../art/SB_H_segue_custom_2x.png

A custom segue enabling you to write your own behaviors.

Push (Deprecated)

../art/SB_H_segue_push_2x.png

Present the content by pushing it onto the current stack of view controllers.

Modal (Deprecated)

../art/SB_H_segue_modal_2x.png

Present the content modally on top of the existing screen. The options are the same as Present Modally.

Popover (Deprecated)

../art/SB_H_segue_popover_2x.png

Present the content as a popover. The options are the same as Present as Popover.

Replace (Deprecated)

../art/SB_H_segue_replace_2x.png

Replace the top view controller on the screen with the new content.


 


(see https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/chapters/StoryboardSegue.html)

 

 

Dismissing a Presented View Controller

 

You dissmiss a ViewController object through the statement

[ViewControllerObject dismissViewControllerAnimated:YES completion: nil];

 

 

SEE developer.apple.com for the Concept of Delegates:

When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it. Although there are several techniques for notifying the presenting view controller that its presented view controller should be dismissed, the preferred technique is delegation. For more information, see Using Delegation to Communicate with Other Controllers.

 

 

 

 

© Lynne Grewe