當我使用我的類AlertView
來顯示UIAlertView
時,我將代理設置爲此類對象,並且期望alertView:clickedButtonAt
運行,但是當我單擊UIAlertView
上的按鈕時,它不起作用。爲什麼?謝謝!爲什麼UIAlertView的委託不起作用?
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window : UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window!.rootViewController = UIViewController()
window!.rootViewController!.view.backgroundColor = .blue
window!.makeKeyAndVisible()
let a = AlertView()
a.show()
return true
}
}
class AlertView : UIViewController,UIAlertViewDelegate{
var done : ((_ buttonIndex: Int)->Void)?
func show(){
var createAccountErrorAlert: UIAlertView = UIAlertView()
createAccountErrorAlert.delegate = self
createAccountErrorAlert.title = "Oops"
createAccountErrorAlert.message = "Could not create account!"
createAccountErrorAlert.addButton(withTitle: "Dismiss")
createAccountErrorAlert.addButton(withTitle: "Retry")
createAccountErrorAlert.show()
}
func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int){
print("Why delegate of alert view does not work?")
}
}
不應使用UIAlertView。您應該使用UIAlertController。 UIAlertView在iOS9中已被棄用。 – Fogmeister
感謝您的關注,我只是編輯並修復了舊的遺留代碼 – TofJ