swift - 这个语句在 Swift 中总是计算为 nil 吗?

标签 swift user-interface button

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
}

所以为了回答您的问题,如果设置buttonInitif 分支将被执行。

关于swift - 这个语句在 Swift 中总是计算为 nil 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53552868/

相关文章:

android - 如何创建等宽的按钮?

java - 我怎样才能处理一个按钮上的两个 Action ?

ios - tableView.moveRow 的自定义动画(位于 : to:)

iOS Swift 在 WebView 中显示 Xcassets 图像

android - 有没有办法为AccountKit Android 定制登录UI?

user-interface - 代码部署后如何比较网站的用户界面元素?

java - 如何动态绘制/缩放按钮?

Swift 方法需要分隔符

ios - ScrollView 没有出现

iOS 用户界面 - 这个用户界面是如何在 YouTube 和 Facebook 应用程序中实现的