来自TableViewCell的presentViewController
我有一个TableViewController,TableViewCell和一个ViewController。 我在TableViewCell中有一个button,我想用presentViewController
来呈现ViewController(但是ViewController在Storyboard上没有视图)。 我试过使用:
@IBAction func playVideo(sender: AnyObject) { let vc = ViewController() self.presentViewController(vc, animated: true, completion: nil) }
错误:typesTableViewCell的值没有成员presentViewController
然后,我试了一下
self.window?.rootViewController!.presentViewController(vc, animated: true, completion: nil)
错误:警告:尝试呈现其视图不在窗口层次结构中!
我究竟做错了什么? 我应该怎么做才能从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) }
我收到错误:不鼓励在分离的视图控制器上呈现视图控制器
(请注意,我在TableView后面有一个NavBar&TabBar链。)
我也试过了
self.parentViewController!.presentViewController(vc, animated: true, completion: nil)
同样的错误。
还试过,
self.view.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)
同样的错误
您应该使用protocol
将操作传递回tableViewController
1)在你的单元格类中创build一个protocol
2)使button
操作调用你的protocol
func
3)通过cell.delegate = self
将你的cell's protocol
链接到tableViewController
4)实现cell's protocol
并在那里添加你的代码
let vc = ViewController() self.presentViewController(vc, animated: true, completion: nil)
看来你已经有了一个想法来呈现一个视图控制器,你需要一个视图控制器。 所以这里是你需要做的:
- 创build一个协议,通知单元的控制器该button被按下。
- 在您的单元格中创build一个属性,该属性持有对实现您的协议的委托的引用。
- 在button操作中调用委托中的协议方法。
- 在视图控制器中实现协议方法。
- 在configuration单元格时,将视图控制器作为委托传递给单元格。
这是一些代码:
// 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 //... } //... }
所以self.presentViewController是ViewController中的一个方法。 你得到这个错误的原因是因为你所指的“自我”是tableViewCell。 而tableViewCell没有presentViewController的方法。
我认为你可以使用一些选项:1.在单元格中添加委托和协议,当你点击button时,IBAction将会调用
self.delegate?.didClickButton()
然后在你的tableVC中,你只需要实现这个方法并调用self.presentViewController
2.使用故事板和segue在故事板中,从你的button拖到你想要的VC。
- iOS辅助function隐藏“textField,双击编辑”通知
- 推送通知applicationIconBadgeNumber在ios7中不更新
- 使用Objective-C在类之间传递数据
- NSURLErrorDomain代码= -1012操作无法完成
- UITabBarController以Swift语言设置默认选项卡
- 如何将默认语言设置为阿拉伯语ios swift?
- 从JSON到NSObjects的对象映射库
- 如何截断UITableView Cell TextLabel中的文本,以便它不会隐藏DetailTextLabel?
- 在上传到networking服务器之前,如何从UIImagePickerController旋转video