Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Present and dismiss modal view controller

Can anyone give me the example code that I can use to first present a modal view controller, then dismiss it? This is what I have been trying:

NSLog(@"%@", blue.modalViewController); [blue presentModalViewController:red animated:YES]; NSLog(@"%@", blue.modalViewController); [blue dismissModalViewControllerAnimated:YES]; NSLog(@"%@", blue.modalViewController); 

This code is in viewDidLoad ("blue" and "red" are both subclasses of UIViewController). I expect that I will show the red view and then immediately hide it, with some animation. However this piece of code only presents the modal view and does not dismiss it. Any idea? The first log shows "null" while the two other logs show <RedViewController: 0x3d21bf0>

Another point is, if I put this code in applicationDidFinishLaunching: the red view does not appear at all, and all logs get "null"

like image 868
phunehehe Avatar asked Oct 07 '09 05:10

phunehehe


People also ask

What is the difference between View and Controller?

The view renders presentation of the model in a particular format. The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model.

What is view and view controller?

Specifically, a view controller manages a view hierarchy and the state information needed to keep those views up-to-date. Every UIKit app relies heavily on view controllers to present content, and you frequently define custom view controllers to manage your views and UI-related logic.


2 Answers

First of all, when you put that code in applicationDidFinishLaunching, it might be the case that controllers instantiated from Interface Builder are not yet linked to your application (so "red" and "blue" are still nil).

But to answer your initial question, what you're doing wrong is that you're calling dismissModalViewControllerAnimated: on the wrong controller! It should be like this:

[blue presentModalViewController:red animated:YES]; [red dismissModalViewControllerAnimated:YES]; 

Usually the "red" controller should decide to dismiss himself at some point (maybe when a "cancel" button is clicked). Then the "red" controller could call the method on self:

[self dismissModalViewControllerAnimated:YES]; 

If it still doesn't work, it might have something to do with the fact that the controller is presented in an animation fashion, so you might not be allowed to dismiss the controller so soon after presenting it.

like image 188
Tom van Zummeren Avatar answered Oct 07 '22 00:10

Tom van Zummeren


Swift

Updated for Swift 3

enter image description here

Storyboard

Create two View Controllers with a button on each. For the second view controller, set the class name to SecondViewController and the storyboard ID to secondVC.

Code

ViewController.swift

import UIKit class ViewController: UIViewController {      @IBAction func presentButtonTapped(_ sender: UIButton) {                  let storyboard = UIStoryboard(name: "Main", bundle: nil)         let myModalViewController = storyboard.instantiateViewController(withIdentifier: "secondVC")         myModalViewController.modalPresentationStyle = UIModalPresentationStyle.fullScreen         myModalViewController.modalTransitionStyle = UIModalTransitionStyle.coverVertical         self.present(myModalViewController, animated: true, completion: nil)     } } 

SecondViewController.swift

import UIKit class SecondViewController: UIViewController {          @IBAction func dismissButtonTapped(_ sender: UIButton) {         self.dismiss(animated: true, completion: nil)     } } 

Source:

  • Documentation
  • SO answer
like image 24
Suragch Avatar answered Oct 07 '22 00:10

Suragch