ios - 来自 TableViewCell 的 presentViewController

标签 ios swift uitableview tableview tableviewcell

我有一个 TableViewController、TableViewCell 和一个 ViewController。我在 TableViewCell 中有一个按钮,我想用 presentViewController 呈现 ViewController(但 ViewController 在 Storyboard上没有 View )。我尝试使用:

@IBAction func playVideo(sender: AnyObject) {
        let vc = ViewController()
        self.presentViewController(vc, animated: true, completion: nil)
}

Error: Value of type TableViewCell has no member presentViewController


然后,我尝试了

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

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


我做错了什么?我应该怎么做才能从 TableViewCell 中呈现 ViewController?另外,如何将数据从 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 链。)


我也试过

 self.parentViewController!.presentViewController(vc, animated: true, completion: nil)

同样的错误。


也试过了,

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

同样的错误

最佳答案

您似乎已经知道要呈现 View Controller ,您需要一个 View Controller 。因此,您需要执行以下操作:

  1. 创建一个协议(protocol),通知单元的 Controller 按钮已按下。
  2. 在您的单元格中创建一个属性,该属性包含对实现您的协议(protocol)的委托(delegate)的引用。
  3. 在按钮操作内对您的委托(delegate)调用协议(protocol)方法。
  4. 在您的 View Controller 中实现协议(protocol)方法。
  5. 配置单元格时,将 View Controller 作为委托(delegate)传递给单元格。

这是一些代码:

// 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

        //...
    }
//...
}

关于ios - 来自 TableViewCell 的 presentViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34911022/

相关文章:

ios - 条件绑定(bind)的初始化程序必须具有可选类型,而不是 '[NSObject: AnyObject]' SwiftyJSON

ios - 当应用程序进入后台时如何继续套接字通信(iOS7)?

ios - 调用父类(super class)指定的初始化程序调用子类

swift - 如何在 Swift 3 中保存 UITableViewCell 附件复选标记的状态

ios - 计算两种颜色之间渐变上给定点的颜色?

ios - 从另一个类访问属性

swift - 在调用方法之前添加 subview

ios - 在 Swift 中校准加速度计

ios - Swift:为 CustomTab 重用一个 ViewController

uitableview - swift iOS 中的 TableView 问题