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 do I go directly to second or third tab when coming to the parent vc? #259

Closed
junweimah opened this issue Feb 27, 2018 · 18 comments
Closed

Comments

@junweimah
Copy link

So I have some checking before bringing user to the parent's vc which holds all the tabs.

So lets say in parent tab, I have 3 tabs.

After user is done doing something in a previous screen, I want to bring user to the second tab, how do I open the parent's vc and directly lands user in the second tab?

Thanks

@aman198
Copy link

aman198 commented Mar 2, 2018

Hi,

Create This Method in TabmanViewController class and add below method

Example
class XYZ: TabmanViewController, PageboyViewControllerDataSource {

func scrollToIndex(indexOf:Int) {
    scrollToPage(.at(index: indexOf), animated: false)
}

Usage:
let obj: PagerDirectoryViewController()
obj.scrollToIndex(indexOf:1)

Hope this will Help

@msaps
Copy link
Member

msaps commented Mar 4, 2018

@junweimah are you wanting to set a default page index when the view controller is loaded, or moving within the page view controller after doing something in one of the child screens?

@msaps msaps added the question label Mar 4, 2018
@junweimah
Copy link
Author

I want to do this

within the page view controller after doing something in one of the child screens?

I already successfully done it by using this line :

self.parentPageboyViewController?.scrollToPage(.first, animated: true)

@junweimah junweimah reopened this Mar 5, 2018
@junweimah
Copy link
Author

Hey, after some progression of my project, I found that I need to go to a specific page when I first enter the parent vc, which is what you said here :

wanting to set a default page index when the view controller is loaded

But I want to control it dynamically, which means I need to do some checking before I decide which page to go

@msaps
Copy link
Member

msaps commented Mar 5, 2018

@junweimah this can be achieved with the defaultPage(for:) data source function:

func defaultPage(for pageboyViewController: PageboyViewController) -> PageboyViewController.Page? {
     return .at(index: 2)
}

@junweimah
Copy link
Author

This code works, but the indicator remains at the first tab when I land on my parent vc, how do I set it to whichever index I want?

@msaps
Copy link
Member

msaps commented Mar 8, 2018

@junweimah this could be a bug, are you saying that the tab bar doesn't respect the default page?

@junweimah
Copy link
Author

Yes.

For example after some checking, i want user to go to page 3, yes it indeed landed on page 3, but the indicator remains at page 1.

@msaps
Copy link
Member

msaps commented Mar 10, 2018

@junweimah how are you trying to achieve this? The defaultPage() data source function seems to work perfectly fine for aligning the bar and page view controller in my testing.

@junweimah
Copy link
Author

This is my exact code at parent vc :

func defaultPage(for pageboyViewController: PageboyViewController) -> PageboyViewController.Page? {
        if(Singleton.shared.goToWhichTabFromMeTab == "favourites"){
            return .at(index: 0)
        }else if(Singleton.shared.goToWhichTabFromMeTab == "savedVouchers"){
            scrollToPage(.at(index: 1), animated: true)
            return .at(index: 1)
        }else{
            scrollToPage(.last, animated: true)
            return .at(index: 2)
        }
    }

In this case i only have 3 tabs to choose where to go to.

@msaps
Copy link
Member

msaps commented Mar 11, 2018

@junweimah the scrollToPage() calls are unnecessary. The bar will automatically go to the default page tab. Removing these should fix any issues with the default display.

@junweimah
Copy link
Author

junweimah commented Mar 12, 2018

Same thing happens

I have changed my code to this :

func defaultPage(for pageboyViewController: PageboyViewController) -> PageboyViewController.Page? {
        if(Singleton.shared.goToWhichTabFromMeTab == "favourites"){
            return .at(index: 0)
        }else if(Singleton.shared.goToWhichTabFromMeTab == "savedVouchers"){
//            scrollToPage(.at(index: 1), animated: true)
            return .at(index: 1)
        }else{
//            scrollToPage(.last, animated: true)
            return .at(index: 2)
        }
    }

I landed on page 3, but the indicator is still at page 1

@junweimah
Copy link
Author

I check the example project, and i find that it is working using .at(index: 2), but for some reason my project can't.

The difference I am seeing between the example and mine are

In example project, you have this :

bar.location = .top

In my project, I have this :

bar.location = .preferred
self.embedBar(in: viewCustomBar)

where viewCustomBar is a UIView outlet dragged from storyboard for my bar to embed in.

Could this be the reason causing the bug?

@junweimah
Copy link
Author

Yes it is confirmed.

I changed my codes to this :

//        bar.location = .preferred
        bar.location = .top
        bar.appearance = PresetAppearanceConfigs.forStyle(self.bar.style, currentAppearance: self.bar.appearance, 3)
//        self.embedBar(in: viewCustomBar)

Now he default page works as intended.

But for some reasons I have to use my previous code, which are

bar.location = .preferred
self.embedBar(in: viewCustomBar)

Can you kindly fix this bug?

@msaps
Copy link
Member

msaps commented Mar 20, 2018

@junweimah I can't seem to reproduce it, even with the bar embedded in an external view.

Could you provide a demo project which reproduces it? Would be much appreciated! 🙂

@junweimah
Copy link
Author

junweimah commented Mar 21, 2018

Ok I will try to reproduce for you to test.

By the way in the meantime do you test out with 3 or 4 child vc? Something like this :

'
private func initializeViewControllers(count: Int) {
let storyboard = UIStoryboard(name: "Landing", bundle: Bundle.main)
var viewControllers = UIViewController

    for index in 0 ..< count {
        var viewControllerChild = UIViewController()
        switch index{
        case 0:
            viewControllerChild = storyboard.instantiateViewController(withIdentifier: "Me_Favourites_CollectionView_SBID") as! MeFavouritesCollectionViewController
        case 1:
            viewControllerChild = storyboard.instantiateViewController(withIdentifier: "Wallet_Reload_SBID") as! WalletReloadViewController
        case 2:
            viewControllerChild = storyboard.instantiateViewController(withIdentifier: "Me_Events_CollectionView_SBID") as! MeEventsCollectionViewController
        default :
            break
        }
        viewControllers.append(viewControllerChild)
    }
    self.viewControllers = viewControllers
}

`

@junweimah
Copy link
Author

I HAVE FOUND THE PROBLEM!!!!

I solved it in my project, this is the things that I have changed :

I commented off these lines

        self.bar.items = [Item(title: title 1),
                          Item(title: title 2),
                          Item(title: title 3)]

and used these lines like in the demo project :
In private func initializeViewControllers(count: Int)

I added :

        var barItems = [Item]()
                        barItems.append(Item(title: "Title: \(index + 1)"))
                bar.items = barItems

And that's it. That's all I changed to remove the bug.

But however I still cannot reproduce the bug using the demo project even reversing what I did to solve the bug

I try using

        self.bar.items = [Item(title: title 1),
                          Item(title: title 2),
                          Item(title: title 3)]

and commenting off

        var barItems = [Item]()
                        barItems.append(Item(title: "Title: \(index + 1)"))
                bar.items = barItems

Both way works in the demo. I dont know which part trigger the bug in my project.

Anyway thanks for the help.

@junweimah
Copy link
Author

After solving this bug, I found another bug

I'll open another issue.

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

No branches or pull requests

3 participants