Swift 4 Change viewController In Tab Bar Controller With data IF, Else (API)






This time i share how to make UITabBarController condition IF and ElSE data you get have 2 or more.

1. you must create new file swift in cocoa touch class adn create UItabbarViewController, example HomeTabBarController.swift

import UIKit

class HomeTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        

      }
  }

2. example in apliacetion.

        
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        DispatchQueue.main.async {
            NetworkService<HomePembelianEntity>.process(client: NetworkClient(endPont: .pembelianHome), onSuccess: { dataPembeli in
                self.dataBeli = dataPembeli
                
                if self.dataBeli?.data?.count == 0 {
                    let berandaVc = HomeViewController()
                    let profilVc = ProfilViewController()
                    let pembelianVc = PembelianKosongViewController()
                    let klaimVc = KlaimKosongViewController() // sementara
                    let pengaturanVC = SettingViewController()
                    
                    berandaVc.tabBarItem.title = "Beranda"
                    profilVc.tabBarItem.title = "Profil"
                    pembelianVc.tabBarItem.title = "Pembelian"
                    klaimVc.tabBarItem.title = "Klaim"
                    pengaturanVC.tabBarItem.title = "Pengaturan"
                    
                    berandaVc.tabBarItem.image = UIImage(named: "beranda.png")
                    profilVc.tabBarItem.image = UIImage(named: "profil.png")
                    pembelianVc.tabBarItem.image = UIImage(named: "pembelian.png")
                    klaimVc.tabBarItem.image = UIImage(named: "klaim.png")
                    pengaturanVC.tabBarItem.image = UIImage(named: "pengaturan.png")
                    
                    
                    self.viewControllers = [berandaVc, profilVc, pembelianVc, klaimVc, pengaturanVC]
                    
                } else {
                    let berandaVc = HomeViewController()
                    let profilVc = ProfilViewController()
                    let pembelianVc = PembelianViewController()
                    let klaimVc = KlaimKosongViewController() // sementara
                    let pengaturanVC = SettingViewController()
                    
                    berandaVc.tabBarItem.title = "Beranda"
                    profilVc.tabBarItem.title = "Profil"
                    pembelianVc.tabBarItem.title = "Pembelian"
                    klaimVc.tabBarItem.title = "Klaim"
                    pengaturanVC.tabBarItem.title = "Pengaturan"
                    
                    berandaVc.tabBarItem.image = UIImage(named: "beranda.png")
                    profilVc.tabBarItem.image = UIImage(named: "profil.png")
                    pembelianVc.tabBarItem.image = UIImage(named: "pembelian.png")
                    klaimVc.tabBarItem.image = UIImage(named: "klaim.png")
                    pengaturanVC.tabBarItem.image = UIImage(named: "pengaturan.png")
                    
                    
                    self.viewControllers = [berandaVc, profilVc, pembelianVc, klaimVc, pengaturanVC]
                }
                
            }, onFailure: nil)
        }
    }












No comments: