2016-08-26 40 views
1

程序创建的按钮时,我创建了一个自定义的警报视图与动态创建按钮执行代码。除了我不知道如何使每个按钮做不同的事情按下功能

,一切工作正常。

我的意思是我称之为功能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. 
     }) 

回答

1

你在找什么是封:

在闭合(变量名)结束在更高/全球范围
var some_code_block = { 
     print("my awesome code block!") 
    } 

将这个,那么你就可以运行/改变块随意,只需加入“()”

func addCode() { 
    // Wont execute: 
    some_code_block = { print("new code inserted") } 
} 

func buttonPressed() { 
    // Executes: 
    some_code_block() 
} 

就我个人而言,我使用数组/字典来实时存储/更新代码块。然后您可以遍历/匹配索引/键来获取您想要的代码块。

https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

如果您的问题实际上是使不同的按钮,那么您可以将新创建​​的一个UI按钮的实例存储到一个数组或字典

[UIButton]

数组/词典的按钮,与数组/字典的代码块匹配,当然可以工作......你只需要确保他们在正确的范围/和同步索引/键。

或者,你可以创建一个类/结构保存一个UIButton实例,一个代码块,然后把类/结构到一个数组/词典

// Make your own initializer for your purposes 
struct ButtonStuff { 
    var button: UIButton? 
    var code_block:()? 
} 

// Make empty array that holds type ButtonStuff (global/outer scope) 
var buttons: [ButtonStuff] = [] 

func addButton(pram1:Pram1, pram2:Pram2) { 
    // Make a new button (called locally inside of a func) 
    var new_button = ButtonStuff() 

    // Put stuff in here... 
    // new_button.button = pram1 
    // new_button.code_block = pram2 

    // Add our locally made new_button to the Global buttons array 
    buttons.append(new_buttons) 
} 

注:pram1,pram2可无论你想要什么,但要存储到代码块(无需运行),你需要输入()。如果你想自动运行的代码块,你可以使用类型()->()

有做上述(你没有使用选配)的许多方面;只是一个简单的例子。然后你只需要做一些逻辑/算法来找到正确的按钮,显示它,然后调用代码块。

如果您有任何问题,需要我来编辑我的回答,请在评论中留下一个消息@fluidity

+0

感谢了很多人!它现在有效。 – Lawrence413

+0

NP !!我仍然是新的我自己。很高兴我能帮上忙 – Fluidity