谢谢您阅读此篇。我想要一个功能Swift文件,将我项目的所有功能都放入其中,其他Swift文件可以调用该文件。我试图在功能文件中创建一个警报功能,当我传递特定的字符串时,它将显示特定的警报。当它位于主文件中时它正在工作,但是当我将其移至功能文件时,presentViewController给我一个错误,说“使用未解析的标识符'presentViewController'”。请帮忙!这是我的代码:
在功能文件中:

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.")
}
}

谢谢!

最佳答案

presentViewControllerUIViewController类的实例方法。因此,您不能像这样在您的功能文件上访问它。

您应该像下面那样更改函数:

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.")
   }
}

在这里,您将UIViewController实例传递给此函数,并调用该View Controller类的presentViewController

关于ios - presentViewController在Swift中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27701323/

10-14 11:58