程序創建的按鈕時,我創建了一個自定義的警報視圖與動態創建按鈕執行代碼。除了我不知道如何使每個按鈕做不同的事情按下功能
,一切工作正常。
我的意思是我稱之爲功能addButton
,我通過一個代碼塊(如UIAlertAction()
)。我想要在按下按鈕後執行該代碼,但實際上在加載按鈕時會執行該代碼。
對不起,如果我不清楚,但我不知道我的問題的具體條款。基本上我試圖重現UIAlertView
,我不知道如何完全重拍UIAlertAction
。
這裏的方法(注:這是一個自定義類):
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)
}
這是選擇(我知道這是不正確的):
func buttonPressed(actions actions:() -> Void) {
actions() // How do I get the actions from addButton?
}
這是當我打電話addButton
:
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