Reputation: 396
I have a class ManageCell
, which stores the frames, set the text of labels, etc... Which are the sub-views of an UIView CellView
which is in the 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
anddeleteButton
.
The problem I'm facing is that the deleteButton
does not respond to touches. The deleteButtonPressed:
function is not being called.
code: https://github.com/an23lm/swift-stuff
I'm not sure if this is good practice, any suggestions are welcome.
Upvotes: 1
Views: 159
Reputation: 4176
Of course it's not called, ViewController.self
is a class type, not your View Controller. And if even it was, it's not a good practice. You should use a delegate pattern here with some parameter to be returned back, so you will distinguish which cell delete button was pressed.
Example on your code:
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")
}
}
Upvotes: 1