首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在自定义UITableViewCell中轻触按钮时更改视图

如何在自定义UITableViewCell中轻触按钮时更改视图
EN

Stack Overflow用户
提问于 2014-12-18 04:42:52
回答 2查看 122关注 0票数 0

通常,我会像这样改变视图:

代码语言:javascript
复制
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中更改视图?

EN

回答 2

Stack Overflow用户

发布于 2014-12-18 04:48:31

你有没有试过添加为子视图?

示例:

代码语言:javascript
复制
var tableViewCell = UITableViewCell(frame: aFrame)

tableViewCell.addSubview(aView)
票数 1
EN

Stack Overflow用户

发布于 2014-12-18 13:15:08

为什么在UITableViewCell子类中这样做?您应该在ViewController中进行视图管理。

在视图控制器中:

代码语言:javascript
复制
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?

干杯!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27534506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档