Ios 从后台返回应用程序时未调用自定义委托方法
我一直在开发一个应用程序,需要在UITableViewCell中放置UICollectionView。每个UICollectionViewCell都有两个按钮,我需要在TableViewController类中处理它们。因此,在设置每个CollectionViewCell时,我向这些按钮添加了目标,以调用TableViewCell类中的方法。然后,我有一个从该方法调用的自定义委托方法,并且我已经在TableViewController类中实现了该方法。除非我按下设备上的home(主页)按钮,然后返回应用程序,否则一切正常。仍在调用TableViewCell类中的方法,但不是我的TableViewController类中的方法Ios 从后台返回应用程序时未调用自定义委托方法,ios,swift,uitableview,swift3,uicollectionview,Ios,Swift,Uitableview,Swift3,Uicollectionview,我一直在开发一个应用程序,需要在UITableViewCell中放置UICollectionView。每个UICollectionViewCell都有两个按钮,我需要在TableViewController类中处理它们。因此,在设置每个CollectionViewCell时,我向这些按钮添加了目标,以调用TableViewCell类中的方法。然后,我有一个从该方法调用的自定义委托方法,并且我已经在TableViewController类中实现了该方法。除非我按下设备上的home(主页)按钮,然后
func handle(button: UIButton?, data: Any?) {
if let msg = data as? String {
print(msg)
}
}
这是我的协议和处理程序
protocol CollectionViewCellButtonPressHandlerDelegate {
func handle(button: UIButton?, data: Any?)
}
class CollectionViewCellButtonPressHandler {
var delegate: CollectionViewCellButtonPressHandlerDelegate?
func handle(button: UIButton?, data: Any?) {
if self.delegate != nil {
delegate?.handle(button: button, data: data)
}
}
}
除了设置TableViewCell,这是我的TableViewController的CellForRowatineXpath方法中的代码
cell.handler = CollectionViewCellButtonPressHandler()
cell.handler.delegate = self
cell.goalCollectionView.reloadData()
这是我在TableViewController类中对委托方法的实现
func handle(button: UIButton?, data: Any?) {
if let msg = data as? String {
print(msg)
}
}
这就是我如何将目标设置为TableViewCell类的cellForRowAtIndexPath方法中的按钮
cell.playButton.addTarget(self, action: #selector(self.play(sender:)), for: .touchUpInside)
func play(sender: UIButton) {
for i in 0..<goals.count {
let indexPath = IndexPath(row: i, section: 0)
if let cell = self.goalCollectionView.cellForItem(at: indexPath) as? GoalCollectionViewCell {
if sender == cell.playButton {
print("Play: ", self.goals[indexPath.row].subskill!)
self.handler.handle(button: sender, data: self.goals[indexPath.row])
}
}
}
}
这是我的TableViewCell类中播放按钮操作的实现
cell.playButton.addTarget(self, action: #selector(self.play(sender:)), for: .touchUpInside)
func play(sender: UIButton) {
for i in 0..<goals.count {
let indexPath = IndexPath(row: i, section: 0)
if let cell = self.goalCollectionView.cellForItem(at: indexPath) as? GoalCollectionViewCell {
if sender == cell.playButton {
print("Play: ", self.goals[indexPath.row].subskill!)
self.handler.handle(button: sender, data: self.goals[indexPath.row])
}
}
}
}
func播放(发送方:ui按钮){
对于0中的i..Swift 3:要将(collectionView或tableView)单元格中的按钮的操作传递给控制器,您必须使用委托。如果此按钮位于另一个(collectionView或tableView)单元格中的单元格中,则需要另一个委托。并且您必须按此层次传递数据。
您可以使用此示例代码,将按钮操作和数据从collectionViewCell
传递到tableViewCell
,然后传递到ViewController
我检查了这个,它总是工作,即使应用程序移动到背景,然后前景
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, TableViewCellPassPressActionToControllerDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
cell.delegate = self
return cell
}
func passAction(button: UIButton?, data: Any?) {
//you have the click of the button in collectionviewcell here
}
}
class TableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, CollectionViewCellButtonPressHandlerDelegate {
@IBOutlet weak var collectionView: UICollectionView!
var delegate: TableViewCellPassPressActionToControllerDelegate?
override func awakeFromNib() {
collectionView.delegate = self
collectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.delegate = self
return cell
}
func handle(button: UIButton?, data: Any?) {
delegate?.passAction(button: button, data: data)
}
}
class CollectionViewCell: UICollectionViewCell {
var delegate: CollectionViewCellButtonPressHandlerDelegate?
@IBAction func press(_ sender: UIButton) {
delegate?.handle(button: sender, data: nil/*any data*/)
}
}
protocol CollectionViewCellButtonPressHandlerDelegate {
func handle(button: UIButton?, data: Any?)
}
protocol TableViewCellPassPressActionToControllerDelegate {
func passAction(button: UIButton?, data: Any?)
}
哪一个符合协议(CollectionViewCellButtonPressHandlerDelegate
)?UITableViewCOntroller或UITableViewCell?TableViewController,因为这是我需要处理按钮按下的地方。CollectionViewCellButtonPressHandler
中的handle
函数是否是一个随机函数,其名称和结构与代理函数func handle(按钮:UIButton?,数据:Any?)相同
?否。CollectionViewCellButtonPressHandler类用于调用具有委托类中相应参数的handle方法,在本例中,该类恰好是我的TableViewController类。您可以在我的TableViewController类的cellForRowAtIndexPath方法中看到该类的作用。好吧,我还有另一个sol为您的案例提供解决方案,您可以找到答案。我希望这会有所帮助。感谢您的帮助,但即使使用您指定的方法,我的问题仍然没有得到解决。从后台返回应用程序后,我可以获取登录TableViewCell的handle方法,但无法获取TableViewController中的handle方法。在按下home按钮之前,按钮我们工作得很好。