2

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!

5 Answers5

6

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.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • I switched it to 'ViewController.presentViewController(pleaseAssessAlert, animated: true, completion: nil)' and now it is returning the following error: "Extra argument 'animated' in call." – DaniSmithProductions Dec 30 '14 at 16:39
  • @user3338042: The same code works for me without any error. Also check the code. I used `viewController` as parameter. In your code it's `ViewController`. So check whether it is correctly typed or not (If not then you are using the Class name, that causes the issue. Change that and it'll work) – Midhun MP Dec 30 '14 at 20:21
  • VeiwController is a class of UIViewController. What should I pass in the place of viewController? – DaniSmithProductions Dec 31 '14 at 04:51
  • You need to pass the `ViewController` class instance. call like : `showAlert("pleaseAssessAlert", self);` if you are calling from a ViewController class. You need to copy above mentioned method, don't change the `viewController` to `ViewController` – Midhun MP Dec 31 '14 at 04:54
5

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)
Community
  • 1
  • 1
Arenzel
  • 1,156
  • 1
  • 11
  • 18
1

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)

Maulik Pandya
  • 2,200
  • 17
  • 26
0

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.")
   }    
}
CodeOverRide
  • 4,431
  • 43
  • 36
0

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

Dilip M
  • 1
  • 3