繁体   English   中英

从ios版SWIFT中的自定义单元格按钮切换到另一个视图

[英]Segueing to another view from a custom cell button in SWIFT for ios app

我正在迅速编写一个ios应用程序。 我有一个ViewController持有一个自定义表格视图,其中包含一个自定义单元格。 每个单元都有一个开关,在激活该开关时,我希望视图可以连接到第二个视图控制器。

我试图通过声明一个故事板变量,使第二个视图控制器实例化,并推送到第二个视图控制器来实现这一点。 我已经在董事会上寻找解决方案,但是似乎没有一个可行。

在类customCellTableViewCell中:UITableViewCell

尝试1:

customCellTableViewCell: UITableViewCell {

var storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let view2: viewController2 = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("View2") as viewController2
switchActive fnc(){

self.window?.rootViewController?.navigationController?.pushViewController(view2, animated: true)

}

…
//rest of code

错误:customCellTableViewCell.Type没有名为故事板的成员

尝试2:

customCellTableViewCell: UITableViewCell {

init(storyboard: UIStoryboard){
        self.storyboard = storyboard
}

required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
}

var storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let view2: viewController2 = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("View2") as viewController2
switchActive fnc(){

self.window?.rootViewController?.navigationController?.pushViewController(view2, animated: true)

}

…
//rest of code

错误:customCellTableViewCell.Type没有名为故事板的成员

尝试3:

customCellTableViewCell: UITableViewCell {


var storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let view2: viewController2 = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("View2") as viewController2

switchActive fnc(){

self.window?.rootViewController?.navigationController?.pushViewController(view2, animated: true)

}

…
//rest of code

将代码放入switchActive fnc内,但不起作用。 用let代替var,无济于事。 尝试了这篇文章中的建议,使用了lazy属性如何快速获取全局指针以查看控制器

我无法弄清楚自己在做什么。 我被卡住了一段时间。

情节提要非常方便。 1)在情节提要中创建2个视图控制器(我想已经完成了)

2)在2个视图控制器之间创建一个segue(按住ctrl键并从第一个ViewController单击到第二个ViewController,它将询问您要使用的segue类型,选择“显示”)。

3)为该Segue提供一个标识符(单击链接,然后单击第4个标签中的右侧面板)

4)在您的第一个View Controller中,当开关被激活时,

self.performSegueWithIdentifier("detail", sender:nil)

用您选择的标识符替换标识符“ detail”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM