如何在 swift ios 中刷新标签栏项目
How to refresh Tab Bar Items in swift ios
我开发了类似 Instagram
的带有标签栏项目的应用程序。在应用程序中,我有 simple user
和 company user
。
我有主要的 ViewController:
MainTabBarController: UITabBarController
有 5 个标签栏项目。每个项目都有自己的 ViewController
我需要刷新 MainTabBarController
当用户 Simple user
时它是 5 项,当用户是 Company user
时它是 4 项。如何在不关闭应用程序的情况下刷新或重新加载?
我已经用 UserDefaults 做了一个解决方案,但需要关闭应用程序。
平台 iOS > 9.0, Swift 3.0
使用setViewControllers(_:animated:)
myTabBarController.setViewControllers(myViewControllers, animated: true)
您可以post根据用户类型通知刷新选项卡,首先在 MainTabBarController 中设置观察者,一旦触发通知,检查用户类型并刷新选项卡
extension Notification.Name {
static let refreshAllTabs = Notification.Name("RefreshAllTabs")
}
class MainTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: .refreshAllTabs, object: nil, queue: .main) { (notification) in
//check if its a normal user or comapny user
if AppUser.shared.type == .normal {
self.viewControllers = [VC1, VC2, VC3, VC4, VC5]
} else {
self.viewControllers = [VC1, VC2, VC3, VC4]
}
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
现在 post 每当用户类型改变时发送通知只需调用
NotificationCenter.default.post(Notification(name: .refreshAllTabs))
通常通知真的一点也不迅速,Swift 鼓励所有程序员使用协议而不是通知...委托或为变量设置 didSet 选项怎么样?您甚至根本不需要通知。我想 TabBar 是在登录后立即推送的,所以你只需制作 class 变量并在 didSet 中设置 viewControllers:
///Considering UserType is enum with cases:
var currentUserType: UserType{
didSet{
currentUserType = .company ? self.viewControllers = /*array with 5 count*/ : self.viewControllers = /*array with 4 counts*/
}
}
现在只需根据 viewControllers 处理其余部分。
我得到了解决方案:
我将 MainTabBarController 分成 3 个 类:
- AnonymUserTabBarController。设置 5 个标签栏项目。
- SimpleUserTabBarController。设置 5 个标签栏项目。
- 公司标签栏控制器。设置 4 个标签栏项目。
并使用动画更改用户 UI:
func selectCompanyUser() {
guard let window = UIApplication.shared.keyWindow else {
return
}
guard let rootViewController = window.rootViewController else {
return
}
let viewController = CompanyTabBarController()
viewController.view.frame = rootViewController.view.frame
viewController.view.layoutIfNeeded()
UIView.transition(with: window, duration: 0.6, options: .transitionFlipFromLeft, animations: {
window.rootViewController = viewController
}, completion: nil)
}
我开发了类似 Instagram
的带有标签栏项目的应用程序。在应用程序中,我有 simple user
和 company user
。
我有主要的 ViewController:
MainTabBarController: UITabBarController
有 5 个标签栏项目。每个项目都有自己的 ViewController
我需要刷新 MainTabBarController
当用户 Simple user
时它是 5 项,当用户是 Company user
时它是 4 项。如何在不关闭应用程序的情况下刷新或重新加载?
我已经用 UserDefaults 做了一个解决方案,但需要关闭应用程序。
平台 iOS > 9.0, Swift 3.0
使用setViewControllers(_:animated:)
myTabBarController.setViewControllers(myViewControllers, animated: true)
您可以post根据用户类型通知刷新选项卡,首先在 MainTabBarController 中设置观察者,一旦触发通知,检查用户类型并刷新选项卡
extension Notification.Name {
static let refreshAllTabs = Notification.Name("RefreshAllTabs")
}
class MainTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: .refreshAllTabs, object: nil, queue: .main) { (notification) in
//check if its a normal user or comapny user
if AppUser.shared.type == .normal {
self.viewControllers = [VC1, VC2, VC3, VC4, VC5]
} else {
self.viewControllers = [VC1, VC2, VC3, VC4]
}
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
现在 post 每当用户类型改变时发送通知只需调用
NotificationCenter.default.post(Notification(name: .refreshAllTabs))
通常通知真的一点也不迅速,Swift 鼓励所有程序员使用协议而不是通知...委托或为变量设置 didSet 选项怎么样?您甚至根本不需要通知。我想 TabBar 是在登录后立即推送的,所以你只需制作 class 变量并在 didSet 中设置 viewControllers:
///Considering UserType is enum with cases:
var currentUserType: UserType{
didSet{
currentUserType = .company ? self.viewControllers = /*array with 5 count*/ : self.viewControllers = /*array with 4 counts*/
}
}
现在只需根据 viewControllers 处理其余部分。
我得到了解决方案: 我将 MainTabBarController 分成 3 个 类:
- AnonymUserTabBarController。设置 5 个标签栏项目。
- SimpleUserTabBarController。设置 5 个标签栏项目。
- 公司标签栏控制器。设置 4 个标签栏项目。
并使用动画更改用户 UI:
func selectCompanyUser() {
guard let window = UIApplication.shared.keyWindow else {
return
}
guard let rootViewController = window.rootViewController else {
return
}
let viewController = CompanyTabBarController()
viewController.view.frame = rootViewController.view.frame
viewController.view.layoutIfNeeded()
UIView.transition(with: window, duration: 0.6, options: .transitionFlipFromLeft, animations: {
window.rootViewController = viewController
}, completion: nil)
}