代码实现点击accessoryButton加载新的viewController

给tableView的每个cell增加accessoryButton后,我们可以设置其segue以达到加载新的viewController并向其传想要的数据(prepareForSegue(sender)),这里提供另一种方法,不使用segue。

override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
    let navigationController = storyboard!.instantiateViewControllerWithIdentifier("ListNavigationController") as UINavigationController

    let controller = navigationController.topViewController as ListDetailViewController
    controller.delegate = self
    let checklist = lists[indexPath.row]
    controller.checklistToEdit = checklist

    presentViewController(navigationController, animated: true, completion: nil)
}

这里我们利用storyboard来找到想要加载的viewController。每个viewController都有一个storyboard属性,通过它的instantiateViewControllerWithIdentifier()方法找到我们想要找的viewController(这里是navigationController),经过一些设置之后,我们用presentViewController(animated,completion)方法将viewController呈现出来,以达到目的。

而identifier(这里是"ListNavigationController")则在story board界面设置。

accessoryButtonPicture.png

设置Storyboard ID我们便可以找到这个viewController。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容