简体   繁体   English

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

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

I am coding an ios app in swift. 我正在迅速编写一个ios应用程序。 I have a ViewController holding a custom table view, which holds a custom cell. 我有一个ViewController持有一个自定义表格视图,其中包含一个自定义单元格。 Each cell has a switch, upon which activating, I would like the view to segue to a second viewcontroller. 每个单元都有一个开关,在激活该开关时,我希望视图可以连接到第二个视图控制器。

I am attempting to achieve this by declaring a storyboard variable, and instatiating the second view controller, and pushing to the second view controller. 我试图通过声明一个故事板变量,使第二个视图控制器实例化,并推送到第二个视图控制器来实现这一点。 I've searched the boards for solutions, however none of them seem to work. 我已经在董事会上寻找解决方案,但是似乎没有一个可行。

Within class customCellTableViewCell: UITableViewCell 在类customCellTableViewCell中:UITableViewCell

Try 1: 尝试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

Error: customCellTableViewCell.Type does not have member named storyboard 错误:customCellTableViewCell.Type没有名为故事板的成员

Try 2: 尝试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

Error: customCellTableViewCell.Type does not have member named storyboard 错误:customCellTableViewCell.Type没有名为故事板的成员

Try 3: 尝试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

Put code inside of switchActive fnc, but does not work. 将代码放入switchActive fnc内,但不起作用。 Replaced var with let, to no avail. 用let代替var,无济于事。 Tried suggestion in this post, using the lazy attribute How to get global pointer to view controller in swift 尝试了这篇文章中的建议,使用了lazy属性如何快速获取全局指针以查看控制器

I cannot figure out what I'm doing wrong. 我无法弄清楚自己在做什么。 I've been stuck for a bit. 我被卡住了一段时间。

Storyboard are very convenient for segueing. 情节提要非常方便。 1) In your storyboard create the 2 views controllers (I supposed it already done) 1)在情节提要中创建2个视图控制器(我想已经完成了)

2) Create a segue between the 2 view controllers(hold ctrl + click from the first ViewController to the second one, It will ask you wich segue type do you want, choose "show"). 2)在2个视图控制器之间创建一个segue(按住ctrl键并从第一个ViewController单击到第二个ViewController,它将询问您要使用的segue类型,选择“显示”)。

3)Give an identifier to that Segue (click on the link and on the right panel in the 4th tab) 3)为该Segue提供一个标识符(单击链接,然后单击第4个标签中的右侧面板)

4)In your first View Controller, when the switch is activated, 4)在您的第一个View Controller中,当开关被激活时,

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

Replace identifier "detail" with the identifier that you chose. 用您选择的标识符替换标识符“ detail”。

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

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