(快速)使用回调函数将数据传递到另一个viewController [英] (Swift) Pass data into another viewController using callBack function

查看:73
本文介绍了(快速)使用回调函数将数据传递到另一个viewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:
我有一个包含许多单元格的tableView.每个单元格都有相同的类型/类,每个单元格都有一个toggleSwitch.
切换开关时,其中一个单元格或更准确地说是第2节第0行的单元格需要将bool类型的变量更新为true/false(如果switch开启=> true).该变量位于第二个VC中.
在下面的代码中,点击哪个单元格无关紧要,它会打印开关打开/开关关闭,但是我仅需要上述单元格.
单元格的班级:

Situation:
I have a tableView with many cells. Every cell has got the same type/class and every cell has got a toggleSwitch.
One of the cells, or more precisely cell in section 2, row 0, when it's switch is toggled, need to update a variable of type bool to true/false(if switch is on => true). That variable is in a second VC.
With the code below, doesn't matter which of the cells is tapped, it prints switch is on/switch is off, but I need that only for a mentioned above cell.
Cell's class:

@IBOutlet weak var labelCell: UILabel!

@IBOutlet weak var cellSwitch: UISwitch!

@IBAction func toggleSwitch(_ sender: Any) {
    if cellSwitch.isOn == true {
        print("switch is on")
    }
    else {
        print("switch is off")
    }
}

在cellForRowAt中:

In cellForRowAt:

case (2,0):
        cell.labelCell.text = "Shuffled"
        let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let cardVC = (mainStoryboard.instantiateViewController(withIdentifier: "CardViewController") as! CardViewController)
        //place for condition, if switch is on/off, put the true/false to secondVC.shuffle
        cardVC.shuffle = true

我的问题是我的回调函数应该是什么样子,我对它们没有经验.以及如何检查此(2,0)单元是否被点击?

My question is how my callback function should look like, I have no experience with them. And how to check if this (2,0) cell is tapped?

推荐答案

将此回调函数声明到tableViewCell文件中,如下所示.

Declare this callBack function into your tableViewCell file as showing bellow.

var callback:((Bool) -> Void)?

@IBAction func toggleSwitch(_ sender: Any) {
    if cellSwitch.isOn == true {
        print("switch is on")
        callback?(true)
    }
    else {
        print("switch is off")
        callback?(false)
    }
}


您可以使用 UITableViewDelegate didSelectRowAt 方法获得行点击.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
if indexPath.section == 2 {
    if indexPath.row == 0 {
        // the tapped detect of (2,0)
    }
}
}


您可以从 cellForRowAt 方法(如显示波纹管)中获得UISwitch的回叫点击动作.


And you can get your callBack tap action of UISwitch from cellForRowAt method like show bellow.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   if indexPath.section == 2 {
      var cell = tableView.dequeueReusableCell(withIdentifier: "yourCell") as! YourCell
      if indexPath.row == 0 {
          //here you can write your callBack closure & your UISwitch's value will retrived here
          cell.callback = { [unowned self] check in
              cardVC.shuffle = check

          }
      }
   }
}

这篇关于(快速)使用回调函数将数据传递到另一个viewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆