我有一個視圖控制器,其中有一個按鈕用于恢復默認設定。在同一個VC中包含了restoreDefaults()函式
。 @IBAction func resetBtnAct(_ sender: Any) {
let alert = AlertGenFromVC(sendingVC: self).generateAlert(
titleIn: "警告!"。
messageIn: "你即將重置為默認設定。你將無法撤消這個動作。"。
actionTitle1: "重置", actionAct1: #selector(self.restoreDefaults), actionTitle2: "取消"/span>, actionAct2: nil)
self.present(
警報。
影片的。false,
完成。nil)
}
我把這個傳遞給另一個類,然后用它來呈現警報控制器
class AlertGenFromVC {
var originVC: AnyObject?
var originTextField: UITextField?
init(sendingVC: AnyObject) {
originVC = sendingVC
}
func generateAlert(titleIn: String, messageIn: String, actionTitle1:String, actionAct1: Selector, actionTitle2:String? , actionAct2:Selector?) -> UIAlertController{
let alert = UIAlertController(
title: titleIn,
message: messageIn,
preferredStyle: .alert)
//>添加第一個動作
let action1 = UIAlertAction( title: actionTitle1, style: UIAlertAction.Style.default, handler: {(action) in actionAct1
})
alert.addAction(action1)
/行動2。
if actionTitle2 ! = nil && actionAct2 != nil {
print("act2"/span>)
let action2 = UIAlertAction(標題:actionTitle2, style: UIAlertAction.Style.default, handler: {action in actionAct2
})
alert.addAction(action2)
} else if actionTitle2 ! = nil && actionAct2 == nil {
print("action 2 nil"/span>)
let action2 = UIAlertAction(標題:actionTitle2, style: UIAlertAction.Style.default, handler: nil)
alert.addAction(action2)
}
let popover = alert.popoverPresentationController
popover?.sourceView = self.originVC as? UIView
popover?.sourceRect = CGRect(x。32, y: 32, width: 64, height: 64)
return警報
}
}
行動1和行動2都顯示了一個警告"'選擇器'型別的運算式未使用"。
警報呈現得很好,但在點擊按鈕時卻沒有任何動作。是我對選擇器的定義不正確還是我做錯了什么?希望收到所有建議。
uj5u.com熱心網友回復:
一個額外的類來創建一個由UIViewController呈現的警報控制器是一個unwifty方法。
一個更好的方法是對UIViewController的擴展
extension UIViewController {
func showAlert(titleIn: String,
messageIn: String,
actionTitle1: String,
actionAct1: (() -> Void)? = nil,
actionTitle2: String?
actionAct2: (() -> Void)? = nil) {
let alert = UIAlertController(
title: titleIn,
message: messageIn,
preferredStyle: .alert)
//>添加第一個動作
let action1 = UIAlertAction( title: actionTitle1, style: .default, handler: { _ in actionAct1?() })
alert.addAction(action1)
/行動2
if let title2 = actionTitle2 {
print(title2)
let action2 = UIAlertAction(title: title2, style: .default, handler: { _ in actionAct2?() })
alert.addAction(action2)
}
let popover = alert.popoverPresentationController
popover?.sourceView= self.view
popover?.sourceRect = CGRect(x。32, y: 32, width: 64, height: 64)
self.present(alert, animated: true, completion: nil)
}
并使用它
func restoreDefaults() {
//做一些事情。
}
func resetBtnAct(_ sender: Any) {
self.showAlert(
titleIn: "警告!"。
messageIn: "你即將重置為默認設定。你將不能撤消這個動作。"。
actionTitle1: "重置"。
actionAct1: restoreDefaults,
actionTitle2: "取消")
}
uj5u.com熱心網友回復:
你可以直接將函式作為一個引數傳遞。比如說像這樣:
let alert = AlertGenFromVC(sendingVC: self).generateAlert(
titleIn: "警告!"。
messageIn: "你即將重置為默認設定。你將無法撤消這個動作。"。
actionTitle1: "重置", actionAct1: self.restoreDefaults, actionTitle2: "取消", actionAct2: nil)
函式generateAlert將看起來像這樣:
func generateAlert(titleIn: String, messageIn: String, actionTitle1:String, actionAct1: @escaping ((UIAlertAction) -> Void), actionTitle2:String? , actionAct2: ((UIAlertAction) -> Void)? = nil) -> UIAlertController {
let alert = UIAlertController(
title: titleIn,
message: messageIn,
preferredStyle: .alert)
//>添加第一個動作
let action1 = UIAlertAction( title: actionTitle1, style: UIAlertAction.Style.default, handler: actionAct1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/307280.html
標籤:
