0
为了避免重复的代码,我想创建一个单独的类文件来处理我的应用程序中的所有错误(通常从后端块返回),并呈现他们在用户可以关闭的警报弹出窗口中。所以,我创建了一个类,并把这个里面:从任何的viewController调用这个函数时在一个单独的类文件中定义UIAlert,然后从任何视图控制器调用它
import Foundation
class Errors {
func errors(errorText: String, currentViewController: UIViewController) {
var alert = UIAlertController(title: "There was an error", message: errorText, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: .Default, handler: nil))
currentViewController.presentViewController(alert, animated: true, completion: nil)
}
}
那么我这样做:
Errors().errors("Error text", currentViewController: GameScreenViewController)
这不会工作和编译器要我加()在GameScreenViewController之后导致应用程序行为不正确。
我想这不是正确的方法来做到这一点,你可以请教如何正确地做到这一点。请在Swift中回答。
查看更新的答案。 self是您称之为错误类的视图控制器。 – 2014-11-24 10:17:41
最优秀,作品优秀 – 2014-11-24 10:19:01
很高兴帮忙@RobertBrax。如果解决了问题,不要忘记接受答案。 (左侧选中标记) – 2014-11-24 10:19:38