【发布时间】:2017-01-11 10:51:41
【问题描述】:
我正在使用 Swift 2.3 进行开发
我有一个 Utils 类,使我能够轻松创建 UIAlertController。
public class Utils {
class func buildAlertInfo(withTitle title: String?, andMessage message: String?, withHandler handler: (UIAlertAction -> Void)?) -> UIAlertController {
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: handler))
return alertController
}
}
它使我能够轻松构建 AlertController:
let alert = Utils.buildAlertInfo(withTitle: "Information", andMessage: "John Snow is dying", withHandler: nil)
self.presentViewController(alert, animated: false, completion: nil)
我现在的问题是我想在我的 Utils 类中创建另一种类型的自定义警报。 例如,带有 Button 的 Alert 将用户导航到特定的 ViewController。
我不知道如何访问自定义类中的 ViewController。也许在点击按钮后将我想要呈现的 ViewController 作为参数传递?
我应该尊重 MVC 模式而不是与 Utils 类中的视图交互吗?
编辑:
我想要的警报应该是这样的:
class func buildAlertInfoWithFavButton(withTitle title: String?, andMessage message: String?, withHandler handler: (UIAlertAction -> Void)?) -> UIAlertController {
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: handler))
alertController.addAction(UIAlertAction(title: "Favorite", style: UIAlertActionStyle.Default, handler: handler))
return alertController
}
OK 操作是相同的,但收藏操作应该将您导航到 FavoriteViewController。
【问题讨论】:
-
您可以使用完成处理程序转到其他控制器 - withHandler 而不是 nil pass 块,您很好
标签: ios swift uialertcontroller