XLPagerTabStrip icon indicating copy to clipboard operation
XLPagerTabStrip copied to clipboard

How to update title of the child dynamically

Open shadowTech827 opened this issue 8 years ago • 13 comments

Before submitting issues ...

  • Make sure you are using XLPagerTabStrip latest release or master branch version.
  • Make sure your Xcode version is the latest stable one.
  • Check if the issue was already reported or fixed. We add labels to each issue in order to easily find related issues. If you found a match add a brief comment "I have the same problem" or "+1".
  • Please do not use the issue tracker for personal support requests. Stack Overflow is a better place for that where a wider community can help you!

When submitting issues, please provide the following information to help maintainers to fix the problem faster:

  • Environment: XLPagerTabStrip, Xcode and iOS version you are using.
  • In case of reporting errors, provide Xcode console output of stack trace or code compilation error.
  • Any other additional detail such as example code that you think it would be useful to understand, reproduce and solve the problem.

shadowTech827 avatar Jul 06 '17 09:07 shadowTech827

any updates guys? need this urgently

shadowTech827 avatar Jul 10 '17 05:07 shadowTech827

I want to implement the same thing. @shadowTech827 any solution?

vaibhav-varshaaweblabs avatar Jul 21 '17 05:07 vaibhav-varshaaweblabs

Hey @vaibhav-varshaaweblabs ,

For the above : I'am saving a track of the new cell and old cell and wrote a little complex logic to support my use case.

When setting up the Tabs:

changeCurrentIndexProgressive = {
          [weak self] (oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: 
      CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
           guard changeCurrentIndex == true else { return }
           oldCell?.label.textColor = .black
           newCell?.label.textColor = UIColor.themeRed()
            print("oldCell: \(String(describing: oldCell?.label?.text)) newCell: \(String(describing: 
       newCell?.label?.text))")
       
        if oldCell == nil {
            if (condition1){
                self?.currentCell = newCell
            }
        }else if newCell == nil{
            if (condition 2){
                self?.completedCell = oldCell
            }else{
                if self?.currentCell == nil {
                    self?.currentCell = oldCell
                }else{
                    self?.completedCell = oldCell
                    
                }
            }
        }

Then updating the headers whenever needed

 func updatetabHeaders()  {
    var current : IndicatorInfoProvider!
    var completed : IndicatorInfoProvider!
    if (condition1){
        current = viewControllers[0] as! IndicatorInfoProvider
        completed = viewControllers[1] as! IndicatorInfoProvider
    }else{
        current = viewControllers[1] as! IndicatorInfoProvider
        completed = viewControllers[2] as! IndicatorInfoProvider
    }
   
    UIView.performWithoutAnimation({ [weak self] () -> Void in
        guard let me = self else { return }
        if self?.currentCell != nil {
            self?.configureCell((self?.currentCell)!, indicatorInfo: current.indicatorInfo(for: me))
        }
        if self?.completedCell != nil {
            self?.configureCell((self?.completedCell)!, indicatorInfo: completed.indicatorInfo(for: me))
        }
    })
}

In your Controller's IndicatorInfo function Change the title as per your need: Like in my case: func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {

    if (tabTitle == nil){
        if condition 1{
            tabTitle = "Title1"
        }else{
            tabTitle = "Title 2"
        }
    }
    return IndicatorInfo(title : tabTitle!)
}

This solution may not be very good as it's a work around for my use case. When come to more tabs it will become complex as tracking the tabs will be too complex (i.e new cell and old cell)

P.S if any other solution do let me know. Thanks in advance

shadowTech827 avatar Jul 24 '17 15:07 shadowTech827

@shadowTech827 do you know how to do it when I have 3 tabs? since I don't know how to update the other cell (not old cell or new cell ) in changeCurrentIndexProgressive

ioio007 avatar Jan 05 '18 07:01 ioio007

if let pagerTabStrip = self?.parent as? ButtonBarPagerTabStripViewController {
    pagerTabStrip.buttonBarView.reloadData()
}

Bo-Bogdan avatar Jan 18 '18 12:01 Bo-Bogdan

I'm looking for this solution too. I need to update the label on tab when a key on firebase is updated. Anybody can help me?

jremerich avatar Jan 22 '18 22:01 jremerich

Hi! I just find the solution. I used the @Bo-Bogdan answer in event of firebase. In childViewController that I wanted to update the tab label, I created a property 'tabBarTitle' that was used in indicatorInfo() to instantiate the IndicatorInfo. So, on update in Firebase I change the tabBarTitle's value and execute the reloadData:

class childViewController: UIViewController, IndicatorInfoProvider {
    var tabBarTitle: String = "Initial value"
    
    func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
        return IndicatorInfo(title: tabBarTitle)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        Database.database().reference()
            .child(YOUR_PATH)
            .observe(DataEventType.value, with: { (snapshot) -> Void in
                let value = snapshot.value as? [String : AnyObject] ?? [:]
                self.tabBarTitle = value[KEY_OF_VALUE] as? String ?? ""
                if let pagerTabStrip = self.parent as? ButtonBarPagerTabStripViewController {
                    pagerTabStrip.buttonBarView.reloadData()
                }
        })
    }
}

jremerich avatar Jan 22 '18 23:01 jremerich

@Bo-Bogdan thank you, this worked!

ioio007 avatar Jan 23 '18 09:01 ioio007

This answer did not work for me as the ViewControllers are not initialized yet until the users advances to the page. It works for updates to the first tab, but any subsequent tabs do not have values.

So, the first tab has the title var set, but the others just have the initial value.

Maybe I am doing something wrong, but I have tried several approaches and all of them seem to have invalid values until the tab is selected.

Any help would be appreciated!!

LilMoke avatar Jul 23 '18 12:07 LilMoke

Thank you @Bo-Bogdan for the answer! Awesome!

vPhongg avatar Aug 24 '18 07:08 vPhongg

@Bo-Bogdan Thanks for the awesome answer, but the tab width doesn't fit update based on new text, any idea?

jackylcs86 avatar Sep 26 '18 07:09 jackylcs86

@Bo-Bogdan Thanks for the awesome answer, but the tab width doesn't fit update based on new text, any idea?

Hi jackylcs86 You found the solution??

mansuu avatar Nov 21 '18 10:11 mansuu

My solution inspired by @Bo-Bogdan `func notifyParent(){ if let parentVC = self.parentVC {

        if let selectedTabVC = parentVC.viewControllers.first(where: {$0 is SelectedTabViewController}) as? SelectedTabViewController {
            let selected = viewModel.selectedStudents.count
            if selected > 0 {
                selectedTabVC.itemInfo = IndicatorInfo(title: "Selected Students(\(selected))")
            } else {
                selectedTabVC.itemInfo = IndicatorInfo(title: "Selected Students")
            }
            parentVC.buttonBarView.reloadData()
        }
    }
}`

aneudysamparo avatar Aug 01 '23 21:08 aneudysamparo