Xcode 我的主视图控制器中的功能不工作

Xcode 我的主视图控制器中的功能不工作,xcode,swift,Xcode,Swift,我正在尝试使用我在主viewController中编写的函数,就是它 func displayAlert(title: String, message: String) { let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) alert.addAction((UIAlertAction(title: "Ok", st

我正在尝试使用我在主viewController中编写的函数,就是它

func displayAlert(title: String, message: String)
{
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction((UIAlertAction(title: "Ok", style: .Default, handler:
        { (action) -> Void in
            self .dismissViewControllerAnimated(true, completion: nil)
    })))

    self.presentViewController(alert, animated: true, completion: nil)
} 
我正在尝试在其他viewController上使用它,调用viewControllerRegistro,因为某些原因无法工作。就是这样

@IBAction func signUp(sender: AnyObject)
{
    //checar que el usuario copero al poner su correo electronico y su contraseña
    if usernameRegistro.text == "" || correoRegistro.text == "" || contraseñaRegistro.text == ""
    {
        ViewController().displayAlert("Informando Error", message: "Porfavor completa los cuadros de registro")
    }
有什么帮助吗? 我正在将xcode 7.0 beta 6与swift 2一起使用。

ViewController()
创建一个新的ViewController实例。该视图控制器不是视图层次结构的一部分(因为您只是创建了它,而没有将其添加到任何地方)

必须在当前可见的视图控制器上调用该方法。因此,该函数不应是
MainViewController
类的一部分。它应该是需要它的ViewController类的一部分。或者,如果在多个视图类中需要它,可以将该函数添加到
UIViewController
的扩展中:

extension UIViewController {
    func displayAlert(title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction((UIAlertAction(title: "Ok", style: .Default, handler:
            { (action) -> Void in
                self .dismissViewControllerAnimated(true, completion: nil)
        })))
        presentViewController(alert, animated: true, completion: nil)
    }
}
通过此扩展,您可以在任何
UIViewController
和UIViewController子类上调用
displayAlert