TableViewCell中的presentViewController [英] presentViewController from TableViewCell

查看:114
本文介绍了TableViewCell中的presentViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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)
}

错误:TableViewCell类型的值没有成员presentViewController

Error: Value of type TableViewCell has no member presentViewController


然后,我尝试了


Then, I tried

self.window?.rootViewController!.presentViewController(vc, animated: true, completion: nil)

错误:警告:尝试显示其视图不在窗口层次结构中!

Error: Warning: Attempt to present whose view is not in the window hierarchy!


我在做什么错?为了从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)
}

我收到错误:不建议在分离的视图控制器上显示视图控制器

I receive error: Presenting view controllers on detached view controllers is discouraged

(请注意,我在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆