繁体   English   中英

如果表行中的某些值已更新,如何切换到另一个视图控制器?

[英]How to switch to another view controller if some value in a table row is updated?

我在一个视图控制器中有一个表视图,其中包含一个类型项的数组,因此,每当某项的某些属性(如信标精度(如下所示))被更新(不断更新)时,我都希望能够切换到另一个视图控制器。 但是我不知道应该为此使用哪种tableview委托方法。

我尝试使用didSelectRowAt方法做到这一点,并且可以,但是我希望能够在不选择的情况下进行转换,只是当某个项目的精度小于我希望能够转换的特定项目的某个值时。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    let item = items[indexPath.row]
    let beac = item.beacon
    let acc = Double(beac!.accuracy)
    if acc < 3.00 {
        if let Level2 = self.storyboard!.instantiateViewController(withIdentifier: "ReportVC") as? UIViewController {
            self.present(Level2, animated: true, completion: nil)
        }
    }
}

这可行!

但是需要一个tableview委托方法或某种其他方式,而我不必实际选择一行,但是它仍然可以执行上述操作。

您可以使用如下计时器调用函数:

var timer = NSTimer()

override func viewDidLoad() {
    scheduledTimerWithTimeInterval()
}

func scheduledTimerWithTimeInterval(){
    // Scheduling timer to Call the function "updateCounting" with the interval of 60 seconds
    timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: Selector("updateCounting"), userInfo: nil, repeats: true)
}

func updateCounting(){
    for item in items {
    let beac = item.beacon
            let acc = Double(beac!.accuracy)
            if acc < 3.00 {
                if let Level2 = self.storyboard!.instantiateViewController(withIdentifier: "ReportVC") as? UIViewController {
                    self.present(Level2, animated: true, completion: nil)
                    break
                }
            }
    }
}

在此功能中,每分钟都会调用一次更新计数,如果此精度较低,则会显示第二个控制器。

您可以确定最适合您的计时器持续时间,如果遇到一些事件或委托进行准确性更改,则也可以在那里显示第二个视图控制器。

希望这可以帮助。

didSet属性观察器用于在刚刚设置或更改属性时执行一些代码。

为数组items添加属性观察器didSet ,并检查准确性。 如果任何信标的精度低于3.00,则可以显示另一个视图控制器

var items: [CLBeacon] = [] {
    didSet {
        if items.contains(where: { $0.accuracy < 3.00 }) {
            if let reportVC = self.storyboard?.instantiateViewController(withIdentifier: "ReportVC") {
                self.present(reportVC, animated: true, completion: nil)
            }
        }
    }
}

如果要将精度低于3.0的特定信标传递给新的视图控制器,请尝试以下操作

var items: [CLBeacon] = [] {
    didSet {
        if let beacon = items.first(where: { $0.accuracy < 3.00 }) {
            if let reportVC = self.storyboard?.instantiateViewController(withIdentifier: "ReportVC") {
                reportVC.selectedBeacon = beacon
                self.present(reportVC, animated: true, completion: nil)
            }
        }
    }
}

暂无
暂无

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

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