swift - 从变量分配 UIButton 目标

标签 swift uibutton swift3

是否可以从变量分配目标?我有一个函数可以添加和删除一个非常多余的目标,我想将它截断一点。我创建了一个 typealias,它复制了 UIButtonTarget 预期的参数,但没有参数就无法分配目标。错误: 调用中缺少参数“action”的参数

 @IBAction func didTapActionBtn(sender: UIButton){
    typealias btnTarget = (_: AnyObject?, action: Selector, for: UIControlEvents)
    let prevTarget: btnTarget = (self, action: #selector(self.showPrevApp), for: .touchUpInside)

    if sender.tag == 0{
      sender.tag = 1
      prevBtn.removeTarget(prevTarget) //*Missing argument for parameter 'action' in call
      nextBtn.removeTarget(self, action: #selector(self.showNextApp), for: .touchUpInside)
    }else{
      sender.tag = 0
      prevBtn.addTarget(self, action: #selector(self.showPrevApp), for: .touchUpInside)
      nextBtn.addTarget(self, action: #selector(self.showNextApp), for: .touchUpInside)
    }
  }

最佳答案

您尝试执行的操作(隐式元组展开行为)曾经在 Swift 中有效,但在 Swift 2.2 中已被弃用并在 Swift 3 中被删除。

这是 Swift 2.2 错误消息:

Passing 3 arguments to a callee as a single tuple value is deprecated

请参阅以下内容以了解删除元组 splat 行为的动机:

SE-0029 Remove Implicit Tuple Splat Behavior

关于swift - 从变量分配 UIButton 目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37902314/

相关文章:

iphone - 使用标准刷新按钮

swift 3 : Unity Integration leads to crash on app lauch

ios - 在 Realm 中,当我更新它时,它只在第 0 个索引中更新如何解决它?

iOS:如何在不使用第三方库或 pod 的情况下在 Swift 中创建可扩展的 TableView

swift - 使用变量作为键访问 Swift 字典

iphone - UIControlStateHighlighted 和 UIControlStateSelected 有什么区别?

swift - XC测试 : how can we inject a class at runtime

ios - 如何在 Objective-C 中使用 buttonWithType 添加按钮样式?

ios - 如何限制按钮点击

ios - iOS Swift 3 Alamofire 4.0.0