从所有视图控制器使用的复杂 UITabbarcontroller 调用函数
Call a function from complex UITabbarcontroller that all view controllers use
我有一个问题需要时间来解决...这是关于我的 UITabBarController class 与其拥有的其余视图控制器交互的方式。
这就是我对我的申请的想法。不知道是不是最方便,所以才求助
首先。在我的场景委托中,我有:
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
window?.rootViewController = TabBar()
*TabBar 是我自定义的 Class UITabBarController。
我的目标是让一个 User 类型在调用需要它的函数时在所有视图控制器中更新。
我尝试了 TabBar() 的 addchild 和协议,但我无法正确设置它。
基本方案
- 场景代表
- 标签栏
- 从数据库接收用户信息并将其保存在用户类型变量中。
- 我定义视图控制器[HomeController, ProfileCont....] 并传递用户类型
- 当我在 Profile 控制器中(例如),并且用户单击一个按钮时,我希望我的 TabBar 的 User 变量被更新,并且也在 Home Controller ProfileController 中更新.. ..(全部)
抱歉,文字太多,但我想以最好的方式解释它。
我附上一些代码。
感谢您的帮助!
class TabBar: UITabBarController {
private var user: User?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
UITabBar.appearance().backgroundColor = .white
UITabBar.appearance().unselectedItemTintColor = .lightGray
tabBar.tintColor = .black
fetchUserData()
}
func fetchUserData() {
print("hey")
guard let currentUid = Auth.auth().currentUser?.uid else { return }
Service.shared.fetchUserData(uid: currentUid) { user in
self.user = user
self.setupVCs()
}
}
func setupVCs() {
guard let user = self.user else { return }
viewControllers = [
createNavController(for: HomeController(user: user), title: NSLocalizedString("Pedidos", comment: ""), image: UIImage(named: "pedidos")!),
createNavController(for: SignUpController(), title: NSLocalizedString("Mapa", comment: ""), image: UIImage(named: "location")!),
createNavController(for: InformePeriodos(user: user), title: NSLocalizedString("Ganancias", comment: ""), image: UIImage(named: "euro")!),
createNavController(for: RetirarEfectivo(user: user), title: NSLocalizedString("Agenda", comment: ""), image: UIImage(named: "calendar")!),
createNavController(for: PerfilController(user: user), title: NSLocalizedString("Perfil", comment: ""), image: UIImage(named: "person")!)
]
}
fileprivate func createNavController(for rootViewController: UIViewController,title: String,image: UIImage) -> UIViewController {
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.title = title
navController.tabBarItem.image = image
return navController
}
}
这是我的配置文件控制器按钮操作
@objc func confirmar(){
//efectivo, and gP and retiro are some data
DriverService.shared.updateRetiro(efectivo: saldoEfectivo, gP: gananciasPagadas, retiro: doubleinputValue) {
guard let currentUid = Auth.auth().currentUser?.uid else { return }
Service.shared.fetchUserData(uid: currentUid) { user in
self.user = user
self.configureUI()
//i need to call FETCHUSERDATA FROM MY TABBAR HERE?
self.removeSpinner()
}
}
}
我认为在我的 profileController 中调用 fetchuserdata 可以解决部分问题
我post解决方案如果能帮助遇到同样问题的每个人
import UIKit
import Firebase
import CoreLocation
class TabBar: UITabBarController {
private var user: User?
override func viewDidLoad() {
super.viewDidLoad()
self.overrideUserInterfaceStyle = .light
fetchUserData()
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(updateUser), name: NSNotification.Name(rawValue: "updateUser"), object: nil)
}
@objc func updateUser() {
print("updateUser")
guard let currentUid = Auth.auth().currentUser?.uid else { return }
Service.shared.fetchUserData(uid: currentUid) { user in
self.user = user
self.setupVCs()
}
}
// MARK: - Helper Functions
func fetchUserData() {
print("hey")
guard let currentUid = Auth.auth().currentUser?.uid else { return }
Service.shared.fetchUserData(uid: currentUid) { user in
self.user = user
self.setupVCs()
}
}
func setupVCs() {
guard let user = self.user else { return }
viewControllers = [
createNavController(for: HomeController(user: user), title: NSLocalizedString("Pedidos", comment: ""), image: UIImage(named: "pedidos")!),
createNavController(for: RiderMap(user: user), title: NSLocalizedString("Mapa", comment: ""), image: UIImage(named: "location")!),
createNavController(for: InformePeriodos(user: user), title: NSLocalizedString("Ganancias", comment: ""), image: UIImage(named: "euro")!),
createNavController(for: CalendarController(user: user), title: NSLocalizedString("Agenda", comment: ""), image: UIImage(named: "calendar")!),
createNavController(for: PerfilController(user: user), title: NSLocalizedString("Perfil", comment: ""), image: UIImage(named: "person")!)
]
}
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
fileprivate func createNavController(for rootViewController: UIViewController,
title: String,
image: UIImage) -> UIViewController {
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.title = title
navController.tabBarItem.image = image
return navController
}
}
然后...从其他视图控制器(无委托)向此标签栏发送通知的函数
@objc func updateUser(){
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateUser"), object: nil)
}
我有一个问题需要时间来解决...这是关于我的 UITabBarController class 与其拥有的其余视图控制器交互的方式。
这就是我对我的申请的想法。不知道是不是最方便,所以才求助
首先。在我的场景委托中,我有:
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
window?.rootViewController = TabBar()
*TabBar 是我自定义的 Class UITabBarController。
我的目标是让一个 User 类型在调用需要它的函数时在所有视图控制器中更新。
我尝试了 TabBar() 的 addchild 和协议,但我无法正确设置它。
基本方案
- 场景代表
- 标签栏
- 从数据库接收用户信息并将其保存在用户类型变量中。
- 我定义视图控制器[HomeController, ProfileCont....] 并传递用户类型
- 当我在 Profile 控制器中(例如),并且用户单击一个按钮时,我希望我的 TabBar 的 User 变量被更新,并且也在 Home Controller ProfileController 中更新.. ..(全部)
抱歉,文字太多,但我想以最好的方式解释它。
我附上一些代码。
感谢您的帮助!
class TabBar: UITabBarController {
private var user: User?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
UITabBar.appearance().backgroundColor = .white
UITabBar.appearance().unselectedItemTintColor = .lightGray
tabBar.tintColor = .black
fetchUserData()
}
func fetchUserData() {
print("hey")
guard let currentUid = Auth.auth().currentUser?.uid else { return }
Service.shared.fetchUserData(uid: currentUid) { user in
self.user = user
self.setupVCs()
}
}
func setupVCs() {
guard let user = self.user else { return }
viewControllers = [
createNavController(for: HomeController(user: user), title: NSLocalizedString("Pedidos", comment: ""), image: UIImage(named: "pedidos")!),
createNavController(for: SignUpController(), title: NSLocalizedString("Mapa", comment: ""), image: UIImage(named: "location")!),
createNavController(for: InformePeriodos(user: user), title: NSLocalizedString("Ganancias", comment: ""), image: UIImage(named: "euro")!),
createNavController(for: RetirarEfectivo(user: user), title: NSLocalizedString("Agenda", comment: ""), image: UIImage(named: "calendar")!),
createNavController(for: PerfilController(user: user), title: NSLocalizedString("Perfil", comment: ""), image: UIImage(named: "person")!)
]
}
fileprivate func createNavController(for rootViewController: UIViewController,title: String,image: UIImage) -> UIViewController {
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.title = title
navController.tabBarItem.image = image
return navController
}
}
这是我的配置文件控制器按钮操作
@objc func confirmar(){
//efectivo, and gP and retiro are some data
DriverService.shared.updateRetiro(efectivo: saldoEfectivo, gP: gananciasPagadas, retiro: doubleinputValue) {
guard let currentUid = Auth.auth().currentUser?.uid else { return }
Service.shared.fetchUserData(uid: currentUid) { user in
self.user = user
self.configureUI()
//i need to call FETCHUSERDATA FROM MY TABBAR HERE?
self.removeSpinner()
}
}
}
我认为在我的 profileController 中调用 fetchuserdata 可以解决部分问题
我post解决方案如果能帮助遇到同样问题的每个人
import UIKit
import Firebase
import CoreLocation
class TabBar: UITabBarController {
private var user: User?
override func viewDidLoad() {
super.viewDidLoad()
self.overrideUserInterfaceStyle = .light
fetchUserData()
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(updateUser), name: NSNotification.Name(rawValue: "updateUser"), object: nil)
}
@objc func updateUser() {
print("updateUser")
guard let currentUid = Auth.auth().currentUser?.uid else { return }
Service.shared.fetchUserData(uid: currentUid) { user in
self.user = user
self.setupVCs()
}
}
// MARK: - Helper Functions
func fetchUserData() {
print("hey")
guard let currentUid = Auth.auth().currentUser?.uid else { return }
Service.shared.fetchUserData(uid: currentUid) { user in
self.user = user
self.setupVCs()
}
}
func setupVCs() {
guard let user = self.user else { return }
viewControllers = [
createNavController(for: HomeController(user: user), title: NSLocalizedString("Pedidos", comment: ""), image: UIImage(named: "pedidos")!),
createNavController(for: RiderMap(user: user), title: NSLocalizedString("Mapa", comment: ""), image: UIImage(named: "location")!),
createNavController(for: InformePeriodos(user: user), title: NSLocalizedString("Ganancias", comment: ""), image: UIImage(named: "euro")!),
createNavController(for: CalendarController(user: user), title: NSLocalizedString("Agenda", comment: ""), image: UIImage(named: "calendar")!),
createNavController(for: PerfilController(user: user), title: NSLocalizedString("Perfil", comment: ""), image: UIImage(named: "person")!)
]
}
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
fileprivate func createNavController(for rootViewController: UIViewController,
title: String,
image: UIImage) -> UIViewController {
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.title = title
navController.tabBarItem.image = image
return navController
}
}
然后...从其他视图控制器(无委托)向此标签栏发送通知的函数
@objc func updateUser(){
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateUser"), object: nil)
}