Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to update title of the child dynamically #412

Open
shadowTech827 opened this issue Jul 6, 2017 · 13 comments
Open

How to update title of the child dynamically #412

shadowTech827 opened this issue Jul 6, 2017 · 13 comments

Comments

@shadowTech827
Copy link

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
Copy link
Author

any updates guys? need this urgently

@vaibhav-varshaaweblabs
Copy link

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

@shadowTech827
Copy link
Author

shadowTech827 commented Jul 24, 2017

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

@ioio007
Copy link

ioio007 commented Jan 5, 2018

@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

@Bo-Bogdan
Copy link

Bo-Bogdan commented Jan 18, 2018

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

@jremerich
Copy link

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
Copy link

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

@ioio007
Copy link

ioio007 commented Jan 23, 2018

@Bo-Bogdan thank you, this worked!

@LilMoke
Copy link

LilMoke commented Jul 23, 2018

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!!

@vPhongg
Copy link

vPhongg commented Aug 24, 2018

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

@jackylcs86
Copy link

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

@mansuu
Copy link

mansuu commented Nov 21, 2018

@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??

@aneudysamparo
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants