ios - 从类实现时 UIButton 不可单击

标签 ios swift uibutton swift2

我有一个 ManageCell 类,它存储框架,设置标签文本等...这是 UIView CellView 的 subview ,它是在 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")
}

User Interaction is enabled on both cellView[index], viewBelowButton and deleteButton.

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

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

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

最佳答案

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

代码示例:

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/

相关文章:

ios - 尝试在 iOS 中添加 subview 时出现 "Array out of bounds"错误

ios - 在 Swift IOS 的 webView 顶部添加边距

swift - 用于多个对象的 tvOS UIFocusGuide

ios - 找不到匹配的配置文件错误 ios xcode 6.3.2

ios - NSKeyedArchiver.archivedData 在 Swift 3 iOS 中不起作用

ios - SIGABRT 与编辑表 - IOS

ios - void 函数中出现意外的非 void 返回值

ios - 在带有 subview 的 UIButton 上显示突出显示

ios - 按下一个 UIButton 会触发另一个 UIButton : I don't want this to happen

ios - UITableViewCell 不响应响应者链