问题描述
我有一个TableViewController,TableViewCell和一个ViewController.我在TableViewCell中有一个按钮,我想用presentViewController
呈现ViewController(但ViewController在故事板上没有视图).我尝试使用:
I have a TableViewController, TableViewCell and a ViewController. I have a button in the TableViewCell and I want to present ViewController with presentViewController
(but ViewController doesn't have a view on storyboard). I tried using:
@IBAction func playVideo(sender: AnyObject) {
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)
}
然后,我尝试了
Then, I tried
self.window?.rootViewController!.presentViewController(vc, animated: true, completion: nil)
我在做什么错?为了从TableViewCell呈现PresentController,我应该怎么做?另外,如何将数据从TableViewCell传递到新呈现的VC?
更新:
protocol TableViewCellDelegate
{
buttonDidClicked(result: Int)
}
class TableViewCell: UITableViewCell {
@IBAction func play(sender: AnyObject) {
if let id = self.item?["id"].int {
self.delegate?.buttonDidClicked(id)
}
}
}
----------------------------------------
// in TableViewController
var delegate: TableViewCellDelegate?
func buttonDidClicked(result: Int) {
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)
}
(请注意,我在TableView后面有一个NavBar和TabBar链.)
(Please note that I have a chain of NavBar & TabBar behind TableView.)
我也尝试过
self.parentViewController!.presentViewController(vc, animated: true, completion: nil)
相同错误.
也尝试过
self.view.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)
相同错误
推荐答案
您应该使用protocol
将操作传递回tableViewController
You should use protocol
to pass the action back to tableViewController
1)在您的单元格类中创建一个protocol
1) Create a protocol
in your cell class
2)使button
操作调用您的protocol
函数
2) Make the button
action call your protocol
func
3)通过cell.delegate = self
4)实施cell's protocol
并在其中添加代码
4) Implement the cell's protocol
and add your code there
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)
这篇关于TableViewCell中的presentViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!