[英]How to perform a segue view from custom UITableViewCell(xib) to another ViewController
我想在我的自定义UITableViewCell上显示一个按钮,将用户带到另一个屏幕上单击。 我试过下面的代码,但是不起作用
子视图:
@IBAction func childScreenButton(sender: AnyObject) {
if let delegate = self.delegate {
delegate.childButtonClickedOnCell(self)
}
}
协议:
protocol childTableCellDelegate: class {
func childButtonClickedOnCell(cell: childViewCell)
}
父级ViewController:
func childButtonClickedOnCell(cell: FeedChildViewCell) {
self.clickedIndexPath = self.feedsTableView.indexPathForCell(cell)
self.performSegueWithIdentifier("toNextScreen", sender: self)
}
在测试断点时,未在子视图上输入“ delegate.childButtonClickedOnCell(self)”。 请让我知道这里做错了什么。 谢谢!!
我怀疑您有几处地方不对,或者定义不正确。
我对此进行了快速测试,并且委托调用工作正常……看看您是否注意到任何不完全相同的东西……
//
// TestTableViewController.swift
//
// Created by DonMag on 4/7/17.
// Copyright © 2017 DonMag. All rights reserved.
//
import UIKit
protocol MyCellDelegate {
func pressedButtonForMyCell(theSender: MyCell)
}
class MyCell: UITableViewCell {
@IBOutlet weak var theLabel: UILabel!
@IBOutlet weak var theButton: UIButton!
var delegate: MyCellDelegate?
@IBAction func childScreenButton(sender: AnyObject) {
delegate?.pressedButtonForMyCell(theSender: self)
}
}
class TestTableViewController: UITableViewController, MyCellDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyCell
cell.theLabel.text = "\(indexPath)"
cell.delegate = self
return cell
}
func pressedButtonForMyCell(theSender: MyCell) {
print("delegate called", theSender)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.