gpt4 book ai didi

swift - 按下在函数中以编程方式创建的按钮时执行代码

转载 作者:行者123 更新时间:2023-11-28 06:35:38 25 4
gpt4 key购买 nike

我创建了一个带有动态创建按钮的自定义警报 View 。

一切正常,除了我不知道如何让每个按钮做不同的事情。

我的意思是我调用函数 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.
})

最佳答案

你要找的是闭包:

  var some_code_block = {
print("my awesome code block!")
}

将它放在更高/全局范围内,然后您可以随意运行/更改 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留言

关于swift - 按下在函数中以编程方式创建的按钮时执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39165178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com