Present and dismiss modal view controller

前端 未结 6 928
广开言路
广开言路 2020-11-28 04:45

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(@\"%@\",         


        
6条回答
  •  忘掉有多难
    2020-11-28 05:33

    Swift

    Updated for Swift 3

    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

提交回复
热议问题