UIAlertView
の表示にクラスAlertView
を使用すると、このクラスオブジェクトにデリゲートを設定してalertView:clickedButtonAt
を実行すると予想されますが、UIAlertView
でabuttonをクリックすると機能しません。どうして?ありがとう!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