从其他 class 访问 TabBarController 中的方法
Access Method in TabBarController from other class
我花了最后几个小时来解决这个问题,但没有任何帮助。我的 LocationHelper 代表如下所示:
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
}
}
当用户将身份验证状态更改为 .authorizedWhenInUse 时,控制台会给出以下输出:
checklocation: 1
auth okay
所以可以说这些方法被正确调用了。但是 tabBar 不会将 isEnabled 更改为 true 或 false。我尝试了很多事情,比如将逻辑放入 NavigationBarControllers 的 viewDidLoad 或每次打开应用程序时检查权限状态,但我认为这不是一个非常优雅的解决方案,所以我实现了委托。我是否必须像我所做的那样实例化 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
}
问题是在这一行
NavigationBarController().checkLocationBasedElements(result: 1)
您创建了另一个实例(NavigationBarController()
)而不是当前实例,因此请尝试将其设为共享单例,或使用通知中心/委托通知 enabling/disabling tabBars
对于您当前的设计,最快的解决方案是:
您的 LocationHelper
应该声明 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
的新实例,它与包含您看到的选项卡的实例不同.
我花了最后几个小时来解决这个问题,但没有任何帮助。我的 LocationHelper 代表如下所示:
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
}
}
当用户将身份验证状态更改为 .authorizedWhenInUse 时,控制台会给出以下输出:
checklocation: 1
auth okay
所以可以说这些方法被正确调用了。但是 tabBar 不会将 isEnabled 更改为 true 或 false。我尝试了很多事情,比如将逻辑放入 NavigationBarControllers 的 viewDidLoad 或每次打开应用程序时检查权限状态,但我认为这不是一个非常优雅的解决方案,所以我实现了委托。我是否必须像我所做的那样实例化 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
}
问题是在这一行
NavigationBarController().checkLocationBasedElements(result: 1)
您创建了另一个实例(NavigationBarController()
)而不是当前实例,因此请尝试将其设为共享单例,或使用通知中心/委托通知 enabling/disabling tabBars
对于您当前的设计,最快的解决方案是:
您的
的一个实例LocationHelper
应该声明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
的新实例,它与包含您看到的选项卡的实例不同.