首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >presentViewController来自TableViewCell

presentViewController来自TableViewCell
EN

Stack Overflow用户
提问于 2016-01-20 21:33:53
回答 4查看 11.9K关注 0票数 3

我有TableViewController,TableViewCell和ViewController。我在TableViewCell中有一个按钮,我想向ViewController展示presentViewController (但是ViewController在故事板上没有视图)。我试着用:

代码语言:javascript
复制
@IBAction func playVideo(sender: AnyObject) {
        let vc = ViewController()
        self.presentViewController(vc, animated: true, completion: nil)
}

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

然后,我试着

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

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

我做错了什么?我应该怎么做才能从presentViewController获得TableViewCell?另外,如何从TableViewCell将数据传递给新的表示VC?

更新:

代码语言:javascript
复制
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)
}

我收到错误:不鼓励在分离的视图控制器上显示视图控制器。

(请注意,我有一个NavBar & TabBar链在TableView后面。)

我也试过

代码语言:javascript
复制
 self.parentViewController!.presentViewController(vc, animated: true, completion: nil)

同样的错误。

也试过了,

代码语言:javascript
复制
self.view.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)

相同误差

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-01-20 21:42:30

您应该使用protocol将操作传递回tableViewController

1)在单元格类中创建protocol

2)让button动作调用您的protocol函数

3)用cell's protocol连接tableViewController中的cell.delegate = self

4)实现cell's protocol并在那里添加代码

代码语言:javascript
复制
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)
票数 7
EN

Stack Overflow用户

发布于 2016-01-20 21:49:39

似乎您已经有了这样的想法:要呈现视图控制器,就需要一个视图控制器。所以你需要做的是:

  1. 创建一个协议,该协议将通知单元格的控制器按钮已按下。
  2. 在单元格中创建一个属性,该属性包含对实现协议的委托的引用。
  3. 在按钮操作中调用委托上的协议方法。
  4. 在视图控制器中实现协议方法。
  5. 配置单元格时,将视图控制器作为委托传递给单元格。

下面是一些代码:

代码语言:javascript
复制
// 1.
protocol PlayVideoCellProtocol {
    func playVideoButtonDidSelect()
}

class TableViewCell {
// ...

// 2.
var delegate: PlayVideoCellProtocol!

// 3.
@IBAction func playVideo(sender: AnyObject) {
    self.delegate.playVideoButtonDidSelect()
}

// ...
}


class TableViewController: SuperClass, PlayVideoCellProtocol {

// ...

    // 4.
    func playVideoButtonDidSelect() {
        let viewController = ViewController() // Or however you want to create it.
        self.presentViewController(viewController, animated: true, completion: nil)
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell {
        //... Your cell configuration

        // 5.
        cell.delegate = self

        //...
    }
//...
}
票数 13
EN

Stack Overflow用户

发布于 2020-06-13 16:12:22

我也遇到了同样的问题,在堆栈溢出的某个地方发现了这段代码,但是我不记得它在哪里,所以它不是我的代码,我将在这里展示它。

这就是我想从任何地方显示视图控制器时使用的方法,它会给出一些提示,keyWindow已经禁用,但是工作正常。

代码语言:javascript
复制
extension UIApplication
{

    class func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?
    {
        if let nav = base as? UINavigationController
        {
            let top = topViewController(nav.visibleViewController)
            return top
        }

        if let tab = base as? UITabBarController
        {
            if let selected = tab.selectedViewController
            {
                let top = topViewController(selected)
                return top
            }
        }

        if let presented = base?.presentedViewController
        {
            let top = topViewController(presented)
            return top
        }
        return base
    }
}

你可以在任何地方使用它,在我的例子中,我用:

代码语言:javascript
复制
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let vc = storyboard.instantiateViewController(withIdentifier: "WeekViewController")
    UIApplication.topViewController()?.navigationController?.show(vc, sender: nil)
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34911022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档