How to use modal views in swift?
Asked Answered
G

1

5

When adding an account to Mail (in preferences), you get a modal view, like this: Example Image, source: kb.xcentric.com/images/ipad12.jpg

My question is, how do I replicate this programatically? In other words, how do I display a modal UIView over a presenting view?

Here's what I have:

import UIKit


class ViewController: UIViewController {



@IBAction func addCard(sender: AnyObject) {
    var addContact : secondViewController = secondViewController()
    self.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal 
    self.modalPresentationStyle = .CurrentContext // Display on top of current UIView
    self.presentViewController(addContact, animated: true, completion: nil)
}
//Code goes on to do other unrelated things

Also, I've done the following:

  1. Created the View Controller in Interface builder.
  2. Connected the BarButtonItem "add contact" to the viewcontroller, by control-clicking, dragging to the viewcontroller of the view I want to display, and selecting "modal" in the drop down.
  3. Set the Class and StoryBoard UI of the presented Viewcontroller to secondViewController.

The expected behavior is that, when the UIBarButton "Add Contact" (which successfully triggers @IBAction func addCard(sender: AnyObject) in the code above) is pressed, secondViewController is presented as a modal view above the main view.

When I run the code above, I get the error "Use of undeclared type secondViewController"

What am I doing wrong?

NOTE: This is a re-ask of an earlier question in which I asked a similar, but slightly different question. I checked on meta, and I think it is OK - I don't want to invalidate the answers on the original question, as this is asking something slightly different. Also, in case it helps, I found some similar questions - in obj C. How do I do that in swift?

Glynnis answered 9/8, 2014 at 22:17 Comment(5)
If you plan to use iOS8, you can have a look at the WWDC 2014 video "Session 228 - A look inside presentation controllers" that deals with the UIPresentationController class.Astri
What is secondViewController? Do you have that class defined?Chaudoin
@Chaudoin I set secondViewController as the class in interface builder (In the "set class" field.) of the view controller I want to be presented. Is there anything else I need to do?Glynnis
can you paste the code of your secondViewController file?Chaudoin
@Chaudoin I don't have a secondViewController file - I thought I could just control the presented view from the main view controller with the (incorrect) code above. Do I need to have a second viewcontroller for the presented view?Glynnis
E
9

Try this :)

@IBAction func addCard(sender: AnyObject) {
        self.modalTransitionStyle = UIModalTransitionStyle.coverVertical 
        // Cover Vertical is necessary for CurrentContext 
        self.modalPresentationStyle = .currentContext 
        // Display on top of    current UIView
        self.present(secondViewController(), animated: true, completion: nil)
}
Emelia answered 13/2, 2015 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.