当前位置: 首页 > 面试题库 >

TableViewCell中的presentViewController

高功
2023-03-14
问题内容

我有一个TableViewController,TableViewCell和一个ViewController。我在TableViewCell中有一个按钮,并且想向其中显示ViewController
presentViewController(但ViewController在故事板上没有视图)。我尝试使用:

@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)

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

我究竟做错了什么?
我应该怎么做才能从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链。)

我也试过

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

同样的错误。

也尝试过

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

同样的错误


问题答案:

您应该protocol用来将动作传回tableViewController

1)protocol在您的单元格班级中创建一个

2)使button动作成为您的protocol功能

3)将您cell's protocoltableViewController通过cell.delegate = self

4)实施cell's protocol并在其中添加代码

let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)


 类似资料:
  • 自定义UITableViewCell。在列表单元里面实现2*2的方格,每个方格有不同的内容。类似于twitter里面的个人数据统计。 [Code4App.com]

  • 实现的效果是,用户用手指从左到右划过列表中的任意一行,此行会有“撕开”的效果,显示其他信息,用户手指反方向划过,则复原。 [Code4App.com]

  • 在输入键盘上加入UIPicker控件。表单还会根据弹出的键盘动态调整位置,从而不会让弹出的键盘遮盖住表单,也就是说,当弹出的键盘挡住界面某些内容时,页面会整体往上移动,从而显露出被遮挡的内容。 [Code4App.com]

  • Animated-TableViewCell 是一个实现了动画单元格的示例项目。

  • 问题内容: 题: 如何使UITableViewCell高度动态变化UICollectionViewCell? 查看层次结构: UIViewController UITableView UITableViewCell UICollectionView UICollectionViewCell1 Label 1 UICollectionViewCell2 Label 2 UICollectionVie

  • 利用iOS6.0的能绑定社会化账号(Facebook,twitter以及新浪微博),实现分享到新浪微博、Facebook,以及twitter的功能。 [Code4App.com]