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")
在沒有發出警報大作。不知道我在做什麼錯我已經閱讀了其他堆棧溢出文章,但我仍然遇到同樣的問題,沒有顯示警報。
我很驚訝你沒有得到一個「視圖不在窗口層次結構」錯誤信息作爲線索。 –