程序创建的按钮时,我创建了一个自定义的警报视图与动态创建按钮执行代码。除了我不知道如何使每个按钮做不同的事情按下功能
,一切工作正常。
我的意思是我称之为功能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