presentViewController在Swift中不起作用

时间:2014-12-30 07:31:18

标签: ios swift xcode6 presentviewcontroller

感谢您阅读本文。我想有一个函数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.")
}    
}

谢谢!

5 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:5)

在Swift 3中:

方法presentViewController已替换为present

您可以像以前一样使用它:

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

答案 2 :(得分:1)

首先,您需要检查您的NavigationController是否合适? 如果是,则以下是present和dismiss presentviewcontroller的代码

用于展示PresentViewController:

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

关闭Presentviewcontroller

self.dismissViewControllerAnimated(true, completion: nil)

答案 3 :(得分:0)

我会说上面的MidHun MP方法但是,如果你正在寻找另一种方法来做到这一点而不引入UIViewController那么:

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

答案 4 :(得分:0)

在使用 layoutsubviewsself.view 时,呈现和导航视图控制器的 viewcontroller.view 函数存在问题,因此必须避免使用这些函数。

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