我花了最后几个小时来解决这个问题,但没有任何帮助。我的LocationHelper Delegate看起来像这样:
func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
print("not terminated")
break
case .authorizedWhenInUse, .authorizedAlways:
NavigationBarController().checkLocationBasedElements(result: 1)
print("auth okay")
break
case .restricted, .denied:
NavigationBarController().checkLocationBasedElements(result: 2)
print("auth denied")
break
}
}
当授权状态发生变化时,我可以在控制台上正确看到打印输出。我的NavigationBarController.checkLocationBasedElements看起来像这样:
func checkLocationBasedElements(result: Int) -> Void{
if(result == 2){
print("checklocation: 2")
tabBar.items?[1].isEnabled = false
tabBar.items?[2].isEnabled = false
}
if(result == 1){
//auth ok
print("checklocation: 1")
tabBar.items?[1].isEnabled = true
tabBar.items?[2].isEnabled = true
}
}
当用户将auth-status更改为.authorizedWhenInUse时,控制台会提供此输出:
checklocation: 1
auth okay
所以可以说方法被正确调用了。但tabBar不会将isEnabled更改为true或false。我尝试了很多东西,例如将逻辑放入NavigationBarControllers的viewDidLoad或每次应用程序打开时检查权限状态,但这不是一个非常优雅的解决方案,我认为这样我实现了委托。我是否必须实现NavigationBarController,否则就像我已经完成它或你会推荐什么?
提前致谢! 编辑: NavigationBarController:
import UIKit
class NavigationBarController: UITabBarController {
//let locationHelper = LocationHelper()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let iconSize = CGSize(width: 35, height: 35)
let iconUnselected = UIColor.white
let iconSelected = UIColor.gray
//Start
tabBar.items?[0].title = "Start"
tabBar.items?[0].setFAIcon(icon: .FAHome, size: iconSize, textColor: iconUnselected, backgroundColor: .clear, selectedTextColor: iconSelected, selectedBackgroundColor: .clear)
//Live
tabBar.items?[1].title = "Live"
tabBar.items?[1].setFAIcon(icon: .FATachometer, size: iconSize, textColor: iconUnselected, backgroundColor: .clear, selectedTextColor: iconSelected, selectedBackgroundColor: .clear)
//Messen
tabBar.items?[2].title = "Messen"
tabBar.items?[2].setFAIcon(icon: .FAClockO, size: iconSize, textColor: iconUnselected, backgroundColor: .clear, selectedTextColor: iconSelected, selectedBackgroundColor: .clear)
//Ergebnisse
tabBar.items?[3].title = "Ergebnisse"
tabBar.items?[3].setFAIcon(icon: .FAWindowRestore, size: iconSize, textColor: iconUnselected, backgroundColor: .clear, selectedTextColor: iconSelected, selectedBackgroundColor: .clear)
//Check GPS Permission
//self.checkLocationBasedElements(result: LocationHelper.shared.checkStatus())
}
func checkLocationBasedElements(result: Int) -> Void{
//look above
}
答案 0 :(得分:0)
问题在于这一行
NavigationBarController().checkLocationBasedElements(result: 1)
您创建了另一个实例(NavigationBarController()
)而不是提供的实例,因此请尝试将其设为共享单例,或使用通知中心/委托来通知启用/禁用tabBars
答案 1 :(得分:0)
对于您当前的设计,最快的解决方案是:
您的NavigationBarController
应声明class LocationHelper {
//...
weak var myTabBarController: NavigationBarController?
//...
}
myTabBarController
NavigationBarController
应从class NavigationBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
LocationHelper.shared.myTabBarController = self
//...
}
}
:
locationManager(_ manager:didChangeAuthorization:)
您的myTabBarController?.checkLocationBasedElements(result:)
应致电
NavigationBarController().checkLocationBasedElements(result:)
而不是
func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
print("not terminated")
case .authorizedWhenInUse, .authorizedAlways:
myTabBarController?.checkLocationBasedElements(result: 1)
print("auth okay")
case .restricted, .denied:
myTabBarController?.checkLocationBasedElements(result: 2)
print("auth denied")
}
}
即。
NavigationBarController().checkLocationBasedElements(result:)
基本上当你checkLocationBasedElements(result:)
执行NavigationBarController
时,Swift
的新实例与包含您看到的标签的实例不同。