Ios 无法在自定义单元格中使用UISwitch移动到新控制器

Ios 无法在自定义单元格中使用UISwitch移动到新控制器,ios,swift,uiviewcontroller,segue,Ios,Swift,Uiviewcontroller,Segue,我正在尝试使用UISwitch上的更改事件从当前ViewController移动到新的ViewController。我的UISwitch已在我的自定义视图中为我的UITableView中的自定义单元格注册。在调用my View控制器中的类时注册该操作,如下所示 import UIKit public class CustomTableViewCell: UITableViewCell { @IBOutlet weak var operatedSwitch: UISwitch!

我正在尝试使用UISwitch上的更改事件从当前ViewController移动到新的ViewController。我的UISwitch已在我的自定义视图中为我的UITableView中的自定义单元格注册。在调用my View控制器中的类时注册该操作,如下所示

import UIKit

public class CustomTableViewCell: UITableViewCell {  
    @IBOutlet weak var operatedSwitch: UISwitch!

    @IBAction func operatedSwitchChange() {
        updateValveOps.valveUpdate()
    }
import UIKit

class updateValveOps {
    class func valveUpdate() {
        let valveOps = ValveOperationsController()
        valveOps.ValveOpsUpdate()
    }
}

class ValveOperationsController: UIViewController {
.
.
func ValveOpsUpdate() {
    performSegueWithIdentifier("ValveOpsToUpdateSegue", sender: nil)
}
当它到达我的ViewController中的我的类时,它调用主类中的一个方法,该方法应移动到我的新ViewController,如下所示

import UIKit

public class CustomTableViewCell: UITableViewCell {  
    @IBOutlet weak var operatedSwitch: UISwitch!

    @IBAction func operatedSwitchChange() {
        updateValveOps.valveUpdate()
    }
import UIKit

class updateValveOps {
    class func valveUpdate() {
        let valveOps = ValveOperationsController()
        valveOps.ValveOpsUpdate()
    }
}

class ValveOperationsController: UIViewController {
.
.
func ValveOpsUpdate() {
    performSegueWithIdentifier("ValveOpsToUpdateSegue", sender: nil)
}

但是,这会导致Sigabrt错误。我还尝试过从当前视图推送到新的视图控制器,但由于某种原因,它会返回到调用视图控制器!我做错了什么?

在您的
@iAction func operatedSwitchChange(){..}

UIApplication.sharedApplication().keyWindow?.visibleViewController()?.performSegueWithIdentifier("ValveOpsToUpdateSegue", sender: nil)
并将此扩展名添加到项目中(将其复制粘贴到新的swift文件中):


在您的
@IBAction func operatedSwitchChange(){..}

UIApplication.sharedApplication().keyWindow?.visibleViewController()?.performSegueWithIdentifier("ValveOpsToUpdateSegue", sender: nil)
并将此扩展名添加到项目中(将其复制粘贴到新的swift文件中):


我设法找到了一种方法,通过使用协议使其工作。首先,我在ValveOperationController中添加了一个协议,并将其引用如下

protocol CustomCellDelegator {
    func callSegueFromCell()
}

class ValveOperationsController: UIViewController, CustomCellDelegator {
然后,我在cellForRowInIndexPath中的单元格中添加了一个委托

    cell.delegate = self
然后,我将协议中调用的方法添加到我的ViewController中

func callSegueFromCell() {
    performSegueWithIdentifier("ValveOpsToUpdateSegue", sender: nil )
}
然后转到CustomTableViewCell,我添加了我的代理

var delegate:CustomCellDelegator!
然后在交换机更改时调用的事件中,我将调用添加到协议中

if(self.delegate != nil){ //Just to be safe.
        self.delegate.callSegueFromCell()
}

当事件被调用时,当交换机更改时,它调用协议,该协议将其传递给我的方法,并且Segue成功运行

我设法找到一种方法,通过使用协议使其工作。首先,我在ValveOperationController中添加了一个协议,并将其引用如下

protocol CustomCellDelegator {
    func callSegueFromCell()
}

class ValveOperationsController: UIViewController, CustomCellDelegator {
然后,我在cellForRowInIndexPath中的单元格中添加了一个委托

    cell.delegate = self
然后,我将协议中调用的方法添加到我的ViewController中

func callSegueFromCell() {
    performSegueWithIdentifier("ValveOpsToUpdateSegue", sender: nil )
}
然后转到CustomTableViewCell,我添加了我的代理

var delegate:CustomCellDelegator!
然后在交换机更改时调用的事件中,我将调用添加到协议中

if(self.delegate != nil){ //Just to be safe.
        self.delegate.callSegueFromCell()
}

当事件被调用时,当交换机更改时,它调用将其传递给my method的协议,并且Segue成功运行

如果您遵循命名约定(例如,以大写字母开始calss name)将有很大帮助如果您遵循命名约定(例如,以大写字母开始calss name)将有很大帮助谢谢您的答复是,似乎存在语法错误,因为visibleViewController不存在。我用inputViewController替换了它,但它返回了一个EXC_BAD_指令错误。感谢更新,代码运行时没有出现任何问题,但仍然使用UISwitch返回调用视图。它点击的第一行是5,然后是5,最后是3,这意味着vc在最后一次通过时为零。感谢您的回复,似乎有语法错误,因为visibleViewController不存在。我用inputViewController替换了它,但它返回了一个EXC_BAD_指令错误。感谢更新,代码运行时没有出现任何问题,但仍然使用UISwitch返回调用视图。它碰到的第一行是5,然后又是5,最后是3,这意味着vc在最后一次通过时为零。