我有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)
错误:警告:尝试显示其视图不在窗口层次结构中!
我做错了什么?我应该怎么做才能从presentViewController获得TableViewCell?另外,如何从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)
}我收到错误:不鼓励在分离的视图控制器上显示视图控制器。
(请注意,我有一个NavBar & TabBar链在TableView后面。)
我也试过
self.parentViewController!.presentViewController(vc, animated: true, completion: nil)同样的错误。
也试过了,
self.view.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)相同误差
发布于 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并在那里添加代码
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)发布于 2016-01-20 21:49:39
似乎您已经有了这样的想法:要呈现视图控制器,就需要一个视图控制器。所以你需要做的是:
下面是一些代码:
// 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
//...
}
//...
}发布于 2020-06-13 16:12:22
我也遇到了同样的问题,在堆栈溢出的某个地方发现了这段代码,但是我不记得它在哪里,所以它不是我的代码,我将在这里展示它。
这就是我想从任何地方显示视图控制器时使用的方法,它会给出一些提示,keyWindow已经禁用,但是工作正常。
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
}
}你可以在任何地方使用它,在我的例子中,我用:
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)
}https://stackoverflow.com/questions/34911022
复制相似问题