通常,我会像这样改变视图:
var goBackToMainView:MainViewController = self.storyboard?.instantiateViewControllerWithIdentifier("navigation2") as MainViewController
self.presentViewController(goBackToMainView, animated: true, completion: nil)但是,如果我尝试在用户点击按钮时在自定义UITableViewCell中执行此操作,则会出现类似customTableViewCell does not have a member named 'storyboard' and 'presentViewController'的错误
如何在TableViewCell中更改视图?
发布于 2014-12-18 04:48:31
你有没有试过添加为子视图?
示例:
var tableViewCell = UITableViewCell(frame: aFrame)
tableViewCell.addSubview(aView)发布于 2014-12-18 13:15:08
为什么在UITableViewCell子类中这样做?您应该在ViewController中进行视图管理。
在视图控制器中:
import UIKit
// Take note of UITableViewDelegate, you'll need it to be able to use
// tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
class MyTableViewController: UITableViewController, UITableViewDelegate {
// Assuming that you have a storyboard variable in AppDelegate
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
// .. more code here like viewDidLoad, etc.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var viewController = appDelegate.storyboard.instantiateViewControllerWithIdentifier("MainViewController") as MainViewController
// This will probably appear as a modal
self.presentViewController(viewController, animated: true, completion: nil)
}
}注意:我只是为上面的代码做了一个快速的操场,所以你可能需要解开选项。
如果在导航控制器中嵌入了视图控制器,则可能需要使用展开Segues。有关更多信息,请单击此处:What are Unwind segues for and how do you use them?
干杯!
https://stackoverflow.com/questions/27534506
复制相似问题