動的に作成されたボタンでカスタムアラートビューを作成しました。関数内でプログラムで作成されたボタンを押したときにコードを実行します。
それぞれのボタンを何か違うものにする方法がわからない限り、すべてうまく動作します。
私が意味するのは、関数addButton
を呼び出し、ブロックのコードを(UIAlertAction()
のように)渡すということです。私はボタンを押した後にそのコードを実行したいが、ボタンがロードされると実際に実行される。
ご迷惑をおかけして申し訳ありませんが、私の問題に関する具体的な条件はわかりません。基本的に私はUIAlertView
を再現しようとしましたが、私は完全にリメイクする方法がわかりませんUIAlertAction
。ここで
func buttonPressed(actions actions:() -> Void) {
actions() // How do I get the actions from addButton?
}
そして、私はaddButton
を呼び出すとき、これは次のとおりです。
func addButton(buttonNumber number: Int, title: String, color: UIColor, actions:() -> Void) {
// Initialization
let button = UIButton(frame: CGRectMake(5, self.wrapper.bounds.height-CGFloat(55*number), self.wrapper.bounds.width-10, 50))
// Configuration
button.setTitle(title, forState: .Normal)
button.backgroundColor = color
button.layer.cornerRadius = 10
button.addTarget(self, action: #selector(myCustomView.buttonPressed(actions:)), forControlEvents: .TouchUpInside)
wrapper.addSubview(button)
}
これはセレクタである(私はそれが正しくないことを知っています) :
let alertView = UIStoryboard(name: "Popups", bundle: nil).instantiateViewControllerWithIdentifier("HAlertView") as! HAlertViewVC
prepareAlert(alertView)
alertView.loadAlert(title: "Hold on", message: "You need to add at least the goal before saving", image: "Warning-Icon", numberOfActions: 1)
alertView.addButton(buttonNumber: 1, title: "Close", color: UIColor(red: 246.0/255.0, green: 71.0/255.0, blue: 71.0/255.0, alpha: 1), actions: {
// alertView.dismiss() // This is the problem. That's just a function that hides the alert view but the issue is that it gets executed right away.
})
おかげでたくさんの人がコメントにメッセージを残してください!それは今働く。 – Lawrence413
NP!私はまだ新しい自分。お役に立てて嬉しいです – Fluidity