我確定有一種通用方法,但我無法弄清楚搜索條件。如何在用戶允許位置使用後更新我的視圖
我有一個設置視圖控制器,其中包括一個表視圖,並在一個單元格中打開位置使用的開關。當用戶切換該開關時,我會觸發一個視圖控制器方法來採取必要的操作。
如果他們關閉開關,那麼我告訴我的模型將該選擇存儲在我的NSUserDefaults中,並更新視圖以反映這一點(包括刪除某些表格單元格)。這裏沒有問題。
如果他們打開開關(第一次),我的方法會在用戶允許在系統彈出窗口中使用位置之前觸發並完成。因此該視圖不被更新。這在技術上是正確的,但由於用戶界面沒有更新以反映它們的變化,所以是不好的用戶體驗。
我正在處理模型中位置跟蹤狀態的變化,這是位置委託,所以如果設置視圖被解散並重新打開,所有都應該如此。
我的問題是我如何異步告訴我的視圖更新,假設它仍然顯示。看起來身體肯定它仍然會在屏幕上,除非用戶是超人,儘管在邏輯上它不需要。
更新:添加一些代碼片段,以更好地說明。
以下是這兩家超市我自己的位置的狀態跟蹤(在useLocation變量),並作爲外景經理代表我的模型類的關鍵部分:
public class WorldModel : NSObject, CLLocationManagerDelegate {
var useLocation = false
override init() {
super.init()
let userDefaults = NSUserDefaults.standardUserDefaults()
useLocation = userDefaults.boolForKey("useLocation")
let locationStatus = CLLocationManager.authorizationStatus()
switch locationStatus {
case .NotDetermined:
useLocation = false
case .AuthorizedAlways, .AuthorizedWhenInUse:
break
case .Denied, .Restricted:
useLocation = false
userDefaults.setBool(false, forKey: "useLocation")
}
if useLocation {
setLocationTracking(true)
}
}
// ================================================================================
// Turn on or off location tracking for the model
// ================================================================================
public func setLocationAllowed(allowed: Bool) {
useLocation = allowed
setLocationTracking(allowed)
}
// ================================================================================
// Turn on or off location tracking with the OS
// ================================================================================
public func setLocationTracking(state: Bool) {
if state {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
locationManager.requestWhenInUseAuthorization()
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.startUpdatingLocation()
} else {
locationManager.stopUpdatingLocation()
}
}
// ================================================================================
// Capture location permission changes from the OS
// ================================================================================
public func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
let userDefaults = NSUserDefaults.standardUserDefaults()
switch status {
case .NotDetermined:
useLocation = false
case .AuthorizedAlways, .AuthorizedWhenInUse:
useLocation = true
userDefaults.setBool(true, forKey: "useLocation")
case .Denied, .Restricted:
useLocation = false
userDefaults.setBool(false, forKey: "useLocation")
}
}
}
,這裏是視圖控制器,該控制器管理我想更新的設置視圖。 (只有相關部分試。)
class SettingsViewController2: UIViewController, UITableViewDataSource, UITableViewDelegate {
var theWorldModel : WorldModel?
func allowLocationChange(sender: UISwitch) {
guard let ourWorld = theWorldModel else {
return
}
ourWorld.setLocationAllowed(sender.on)
self.view.setNeedsDisplay()
}
}
的allowLocationChange方法由開關在屏幕上,在關閉的情況下發射具有立竿見影的效果,因爲我直接走的是「關掉」的概念,以我的模型。在「打開」的情況下,我仍然使用我的模型來實現它,但結果不是立即的(由於操作系統彈出請求),所以setNeedsDisplay在模型註冊更改之前設置顯示。
理論上我可以阻止allowLocationChange方法,但在這麼多層次上聽起來像是一個糟糕的主意。
只有描述不夠明白放一些代碼和視覺流@zkarj – Jaimish