我的雨燕2.3斯威夫特 - 如何在自定義按鍵敲擊時AlertController
發展我有一個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的類來創建一個其他類型的自定義報警的。 例如帶有將用戶導航到特定ViewController的按鈕的警報。
我不知道如何訪問自定義類中的ViewController。也許作爲一個參數傳遞我想在點擊按鈕後呈現的ViewController?
我應該尊重MVC模式,而不是與我的Utils類中的View交互?
編輯:
警報我想應該是這樣的:
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而不是零通過塊,你很好 – Miknash