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

ManageCell:

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)
    }
}

ViewController:
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

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

最佳答案

当然它没有被调用,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")
    }
}

关于ios - 从类实现时,UIButton不可单击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32033156/

10-09 16:28