简体   繁体   中英

How to refresh Tab Bar Items in swift ios

I do app like Instagram with tab bar items. In app I have simple user and company user .

I have main ViewController:

MainTabBarController: UITabBarController

with 5 tab bar items. And each item has own ViewController

I need refresh MainTabBarController when user is Simple user it is 5 items and when user is Company user it is 4 items. How to refresh or reload without close app?

One solution I already do with UserDefaults, but need close app.

Platform iOS > 9.0, Swift 3.0

使用setViewControllers(_:animated :)

myTabBarController.setViewControllers(myViewControllers, animated: true)

Usually notifications are really not swifty at all, Swift encourages all programmers to use protocols over Notifications... What about delegation or setting up didSet option for variable? You don't need even the notification at all. I suppose that TabBar is pushed right after the login, so you just make class variable and right in didSet setup the 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*/
}
}

and now just handle according to viewControllers the rest.

I got solution: I divided my MainTabBarController to 3 classes:

  1. AnonymUserTabBarController. Set 5 tab bar items.
  2. SimpleUserTabBarController. Set 5 tab bar items.
  3. CompanyTabBarController. Set 4 tab bar items.

And change user UI with animation:

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)
}

You can post notification to refresh the tabs based on user type, first set the observer in MainTabBarController and once the notification is fired, check the user type and refresh the tabs

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: nil) { (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.
    }

}

now for posting the notification whenever user type changes just call

 NotificationCenter.default.post(Notification(name: .refreshAllTabs))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM