Reputation: 81
I have a table view in one view controller, this contains an array of type items, so whenever some property like the beacon accuracy (shown below) of an item is updated(constantly updating), I want to be able to switch to another view controller. But i don't know which tableview delegate method should I use for this.
I tried to do it with didSelectRowAt method and that works but I want to be able to transition without selecting, just when the accuracy for an item is less than some value for a specific item i want to be able to transition.
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)
}
}
}
This works!
But need a tableview delegate method or some other way where I dont have to actually select a row, but it still performs the above.
Upvotes: 0
Views: 39
Reputation: 1133
You can call a function with a timer like this :
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
}
}
}
}
Here the update counting will be called every minute and in this function and if the accuracy is lesser then the second controller will be presented.
The timer duration you can decide which suits you best and if you are getting some event or delegate for the accuracy change then you can present the second view controller there also.
Hope this helps.
Upvotes: 1
Reputation: 15748
didSet
property observer is used to execute some code when a property has just been set or changed.
Add a property observer didSet
for the array items
, and check the accuracy. If any beacon's accuracy is less than 3.00 you can present another view controller
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)
}
}
}
}
If you want to pass that particular beacon which has accuracy less than 3.0 to the new view controller, try this
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)
}
}
}
}
Upvotes: 0