DaniSmithProductions
DaniSmithProductions

Reputation: 112

presentViewController not working in Swift

Thank you for reading this. I would like to have a functions Swift file where I put all of the functions for my project into, that the other Swift files could call. I am trying to create an alert function in the functions file that, when I pass in a specific string, it shows a specific alert. It was working when it was in the main file, but when I moved it to the functions file, presentViewController is giving me an error, saying "Use of unresolved identifier 'presentViewController'." Please help! Here is my code: in the functions file:

import Foundation
import UIKit

/**********************************************
Variables
***********************************************/
var canTapButton: Bool = false
var tappedAmount = 0

/**********************************************
Functions
***********************************************/

//the alert to ask the user to assess their speed
func showAlert(alert: String) -> Void
{
if(alert == "pleaseAssessAlert")
{
    let pleaseAssessAlert = UIAlertController(title: "Welcome!", message: "If this is your firs time, I encourage you to use the Speed Assessment Tool (located in the menu) to figure which of you fingers is fastest!", preferredStyle: .Alert)
    //ok button
    let okButtonOnAlertAction = UIAlertAction(title: "Done", style: .Default)
        { (action) -> Void in
            //what happens when "ok" is pressed
    }
    pleaseAssessAlert.addAction(okButtonOnAlertAction)

    presentViewController(pleaseAssessAlert, animated: true, completion: nil)
}
else
{
    println("Error calling the alert function.")
}    
}

Thanks!

Upvotes: 2

Views: 19395

Answers (5)

Dilip M
Dilip M

Reputation: 1

Presenting & navigation view controller has a problem with layoutsubviews function while using self.view or viewcontroller.view, so one must avoid those function.

Check: func layoutsubviews not allows to provide the viewcontroller.view to work on it

Upvotes: 0

Maulik Pandya
Maulik Pandya

Reputation: 2230

First, you need to check your NavigationController is appropriate or not? If Yes, then Here is code for present and dismiss presentviewcontroller

For presenting PresentViewController :

let next = self.storyboard?.instantiateViewControllerWithIdentifier("Your view controller identifier") as! Yourviewcontroller self.presentViewController(next, animated: true, completion: nil)

Dismiss Presentviewcontroller

self.dismissViewControllerAnimated(true, completion: nil)

Upvotes: 1

Arenzel
Arenzel

Reputation: 1156

In Swift 3:

The method presentViewController is replaced by present.

You can use it like the old one:

self.present(viewControllerToPresent, animated: true, completion: nil)

Upvotes: 5

CodeOverRide
CodeOverRide

Reputation: 4471

I would say go with MidHun MP method above but, if you are looking for another way to do this without bringing in the UIViewController then:

func showAlert(alert : String) {
   var window: UIWindow?

   if(alert == "pleaseAssessAlert")
   {
       let pleaseAssessAlert = UIAlertController(title: "Welcome!", message: "If this is your firs time, I encourage you to use the Speed Assessment Tool (located in the menu) to figure which of you fingers is fastest!", preferredStyle: .Alert)
       //ok button
       let okButtonOnAlertAction = UIAlertAction(title: "Done", style: .Default)
       { (action) -> Void in
            //what happens when "ok" is pressed
       }
       pleaseAssessAlert.addAction(okButtonOnAlertAction)
       self.window?.rootViewController?.presentViewController(pleaseAssessAlert, animated: true, completion: nil)
   }
   else
   {
       println("Error calling the alert function.")
   }    
}

Upvotes: 0

Midhun MP
Midhun MP

Reputation: 107221

The presentViewController is the instance method of UIViewController class. So you can't access it on your function file like this.

You should change the function like:

func showAlert(alert : String, viewController : UIViewController) -> Void
{
   if(alert == "pleaseAssessAlert")
   {
       let pleaseAssessAlert = UIAlertController(title: "Welcome!", message: "If this is your firs time, I encourage you to use the Speed Assessment Tool (located in the menu) to figure which of you fingers is fastest!", preferredStyle: .Alert)
       //ok button
       let okButtonOnAlertAction = UIAlertAction(title: "Done", style: .Default)
       { (action) -> Void in
            //what happens when "ok" is pressed
       }
       pleaseAssessAlert.addAction(okButtonOnAlertAction)
       viewController.presentViewController(pleaseAssessAlert, animated: true, completion: nil)
   }
   else
   {
       println("Error calling the alert function.")
   }    
}

Here, you are passing a UIViewController instance to this function and calling the presentViewController of that View Controller class.

Upvotes: 6

Related Questions