0
我一直在尝试从另一个类调用我的函数到我的新类中。 原先的功能代码和类显示如下:在Swift中调用另一个类的函数2.1
class ViewController: 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)
})))
self.presentViewController(alert, animated: true, completion: nil)
}
}
我试图调用显示报警功能在这个类像这样:
class logInViewController: UIViewController{
var loginError = ViewController().displayAlert("error", message: "What is wrong")
print(loginError)
or
ViewController().displayAlert("error", message: "What is wrong")
}
当代码运行时,它不显示警报。但是,如果我在从原来的ViewController类的功能块拖我可以调用
displayAlert("error", message: "Some Message")
在没有发出警报大作。不知道我在做什么错我已经阅读了其他堆栈溢出文章,但我仍然遇到同样的问题,没有显示警报。
我很惊讶你没有得到一个“视图不在窗口层次结构”错误信息作为线索。 –