【问题标题】:UIButton not clickable when implemented from a class从类实现时 UIButton 不可点击
【发布时间】:2015-08-16 08:22:31
【问题描述】:

我有一个类ManageCell,它存储框架、设置标签文本等...这是ViewController 中的UIView CellView 的子视图。

管理单元:

import Foundation
import UIKit
class ManageCell {
    var name: UILabel
    var viewBelowButton: UIView
    var deleteButton: UIButton
    init (name: String) {
        self.name = UILabel(frame: CGRectMake(10,15,250,40)
        self.name.text = name
        self.name.sizeToFit()
        self.viewBelowButton = UIButton(frame: CGRectMake(UIScreen.mainScreen.bounds.width, 0, 70, 70)
        //set outside the visible area so that it can be animated in.
        self.viewBelowButton.backgroundColor = UIColor.redColor()
        self.deleteButton = UIButton(frame: CGRectMake(0,0,70,70))
        self.deleteButton.addTarget(ViewController.self, action: "deleteButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
        self.deleteButton.setTitle("delete", forState: .Normal)
    }
}

视图控制器:

var cellView: [UIView] = []
var manageCells: [ManageCell] = []
...
//fill the manageCells array
func setSubViews () {
    for (index, cell) in manageCells.enumerate() {
        cellView.append(UIView(frame: CGRectMake(0, originY, view.bounds.width, 70)
        cellView[index].addSubview(cell.name)
        cellView[index].addSubview(cell.viewBelowButton)
        cell.viewBelowButton.addSubview(cell.deleteButton)
    }
}
func editing () {
    var frame = CGRectMake(view.bound.width - 70, 0, 0, 70)
    for cell in cells {
        UIView.animateWithDuration(0.2, animations: {
            cell.viewBelowButton.frame = frame
        }
    }
}
func deleteButtonPressed(sender: UIButton) {
    print("button pressed")
}

cellView[index]viewBelowButtondeleteButton 上都启用了用户交互。

我面临的问题是deleteButton 不响应触摸deleteButtonPressed: 函数未被调用。

代码:https://github.com/an23lm/swift-stuff

我不确定这是否是好的做法,欢迎提出任何建议。

【问题讨论】:

  • 听起来按钮在其超级视图之一的框架之外。尝试为所有涉及的视图将clipsToBounds 设置为true。如果我是正确的,您将无法再看到按钮(或至少部分按钮)。
  • 在 ViewController 类中添加按钮时,为什么不做 addTarget 呢?这样,您将在同一个类中拥有选择器。还要按照 Aaron 的正确建议检查框架。
  • @GurtejSingh 好点。这是另一个问题,尽管按钮点击甚至没有注册,否则 OP 会崩溃。
  • @AaronBrager 同意!但我怀疑问题出在 ViewController.self 上。我认为这不是一个例子。
  • @GurtejSingh,我将addTarget 移动到ViewController,现在它是cell.deleteButton.addTarget(self, action: "self.deleteButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside),仍然没有。是的,你对坠机的看法是对的,奇怪的是我没有坠机。 @AaronBrager,我确实将viewBelowButton 移动到屏幕的框架中并删除了动画(以确保deleteButton 被初始化为它下方的所有超级视图),并使用了clipToBounds,但没有任何变化。

标签: ios swift uibutton swift2


【解决方案1】:

当然不叫,ViewController.self 是类类型,不是你的View Controller。即使是这样,这也不是一个好习惯。您应该在这里使用委托模式并返回一些参数,这样您就可以区分按下了哪个单元格删除按钮。

代码示例:

protocol ManageCellDelegate: class {
    func manageCellDeletePressed(id: Int)
}

class ManageCell {
    var name: UILabel
    var viewBelowButton: UIView
    var deleteButton: UIButton
    weak var delegate: ManageCellDelegate?
    var id: Int
    init (id: Int, name: String) {
        self.id = id
        self.name = UILabel(frame: CGRectMake(10,15,250,40))
        self.name.text = name
        self.name.sizeToFit()
        self.viewBelowButton = UIButton(frame: CGRectMake(UIScreen.mainScreen().bounds.width, 0, 70, 70))
        //set outside the visible area so that it can be animated in.
        self.viewBelowButton.backgroundColor = UIColor.redColor()
        self.deleteButton = UIButton(frame: CGRectMake(0,0,70,70))
        self.deleteButton.addTarget(self, action: "deleteButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
        self.deleteButton.setTitle("delete", forState: .Normal)
    }

    func deleteButtonPressed(sender: AnyObject) {
        self.delegate?.manageCellDeletePressed(id)
    }
}

class ViewController: UIViewController {
    var cellView: [UIView] = []
    var manageCells: [ManageCell] = []

    func fullManageCells() {
        for id in 0...15 {
            let manageCell = ManageCell(id: id, name: "something")
            manageCell.delegate = self
            manageCells.append(manageCell)
        }
    }
}

extension ViewController: ManageCellDelegate {
    func manageCellDeletePressed(id: Int) {
        println("button with id \(id) pressed")
    }
}

【讨论】:

  • 有一个错误-'ManageCell' does not have a member named 'delegate'self.delegate?.manageCekkDeletePressed(id)cell.delegate = self在一行
  • ViewController.selfClass 类型的一个实例。如果这是唯一的问题,就会出现“无法识别的选择器”崩溃。第一个问题是根本没有触发动作。
  • @an23lm 看来你错过了我的示例中的weak var
  • 哦,是的,我错过了。好的,我现在包含它,现在没有错误,但仍然没有函数调用...
  • 可能是 Xcode 测试版中的一些错误。因为,我在ViewController 中制作了一组按钮,并将它们添加到ViewController 文件中的CellView 中,甚至确保在初始化期间所有按钮的超级视图都在屏幕范围内。奇怪的是,我有另一个按钮,我使用 main.storyboard 设置它工作正常。
猜你喜欢
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
相关资源
最近更新 更多