open var buttonInit: ((_ index: Int) -> UIButton?)?
...
if let button: UIButton = self.buttonInit?(i) {
finButton = button
}else {
let button = UIButton(type: .custom)
button.setTitleColor(button.tintColor, for: [])
button.layer.borderColor = button.tintColor.cgColor
button.layer.borderWidth = 1
button.layer.cornerRadius = buttonHeight/2
finButton = button
}
我在AZDialogViewController 中没有找到任何关于buttonInit 的函数描述| .这是否意味着 button: UIButton = self.buttonInit?(i) 将始终为 nil 而 finButton = button 将不会被执行?
最佳答案
你引用的后半部分代码在setUpButton
方法中:
fileprivate func setupButton(index i:Int) -> UIButton{
if buttonHeight == 0 {buttonHeight = CGFloat(Int(deviceHeight * 0.07))}
let finButton: UIButton
if let button: UIButton = self.buttonInit?(i) {
finButton = button
}else {
let button = UIButton(type: .custom)
button.setTitleColor(button.tintColor, for: [])
button.layer.borderColor = button.tintColor.cgColor
button.layer.borderWidth = 1
button.layer.cornerRadius = buttonHeight/2
finButton = button
}
这个方法在这里调用:
open func addAction(_ action: AZDialogAction){
actions.append(action)
if buttonsStackView != nil{
let button = setupButton(index: actions.count-1)
self.buttonsStackView.addArrangedSubview(button)
//button.frame = buttonsStackView.bounds
button.center = CGPoint(x: buttonsStackView.bounds.midX,y: buttonsStackView.bounds.maxY)
animateStackView()
}
}
从这里我们可以看出,buttonInit
似乎是用来让库的用户指定他们想要什么样的按钮作为操作按钮。另一个证据是 buttonInit
被声明为 open
,因此很可能是客户端代码应该设置它,而不是 AZDialogViewController
.
此外,README
文件显示了这种用法:
Use custom UIButton sub-class:
dialog.buttonInit = { index in //set a custom button only for the first index return index == 0 ? HighlightableButton() : nil }
所以为了回答您的问题,如果您设置buttonInit
,if
分支将被执行。
关于swift - 这个语句在 Swift 中总是计算为 nil 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53552868/