簡體   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