gpt4 book ai didi

ios - 从其他类访问 TabBarController 中的方法

转载 作者:行者123 更新时间:2023-11-30 11:42:49 26 4
gpt4 key购买 nike

我花了最后几个小时来解决这个问题,但没有任何帮助。我的 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 中或每次打开应用程序时检查权限状态,但这不是一个非常优雅的解决方案,因此我实现了委托(delegate)。我是否必须实例化 NavigationBarController,否则就像我所做的那样,或者您会推荐什么?

提前致谢!编辑:导航栏 Controller :

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

}

最佳答案

对于您当前的设计,最快的解决方案是:

  1. 您的 LocationHelper 应声明 NavigationBarController

    的实例
    class LocationHelper {
    //...
    weak var myTabBarController: NavigationBarController?
    //...
    }
  2. 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

    //...
    }

    }
  3. 您的 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:) 时,您正在 NavigationBarController 的新实例上运行 checkLocationBasedElements(result:) > 这与包含您看到的选项卡的不同。

关于ios - 从其他类访问 TabBarController 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49160379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com