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

Get current controller with InfiniteDataSource #224

Closed
mimmo00 opened this issue Jul 24, 2018 · 12 comments
Closed

Get current controller with InfiniteDataSource #224

mimmo00 opened this issue Jul 24, 2018 · 12 comments

Comments

@mimmo00
Copy link

mimmo00 commented Jul 24, 2018

Hi, congratulations for the framework!
I'm using the InfiniteDataSource. I can set the variables of controller during the initial setting.

`extension ContainerViewController: PagingViewControllerInfiniteDataSource {

func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForPagingItem pagingItem: T) -> UIViewController {
    let calendarItem = pagingItem as! CalendarItem
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let currentController = storyboard.instantiateViewController(withIdentifier: "CollectionViewController") as! CollectionViewController
    currentController.currentDate = calendarItem.date
    currentController.delegate = self
    return currentController
}

...`

How can I communicate with the current controller later?

Thanks

@rechsteiner
Copy link
Owner

Hi @mimmo00! You can get the currently selected view controller using the delegate:

func pagingViewController<T>(
    _ pagingViewController: PagingViewController<T>,
    didScrollToItem pagingItem: T,
    startingViewController: UIViewController?,
    destinationViewController: UIViewController,
    transitionSuccessful: Bool)

Does that work for you?

@mimmo00
Copy link
Author

mimmo00 commented Jul 26, 2018

Thanks @rechsteiner for the answer. I use it to get the scroll of the pages. Two questions:

  • do I need to cast the destionationViewController into CollectionViewController, right?
  • I have to use this delegate even if I don’t scroll?

Thanks a lot!

@rechsteiner
Copy link
Owner

do I need to cast the destionationViewController into CollectionViewController, right?

Yeah, that's correct

I have to use this delegate even if I don’t scroll?

Yes, this delegate should also be called if the user selects the items in the menu 🙂

@mimmo00
Copy link
Author

mimmo00 commented Aug 19, 2018

Thanks!

@mimmo00
Copy link
Author

mimmo00 commented Aug 24, 2018

Hi @rechsteiner , unfortunately the delegate is not called on startup. it is only called when you scroll or select a menu item. So I have not found a solution yet :(

@rechsteiner
Copy link
Owner

Hi 👋 If you need the initially selected view controller you should be able to read that directly on the page view controller like this:

pagingViewController.pageViewController.selectedViewController

Does that work for you?

@mimmo00
Copy link
Author

mimmo00 commented Aug 24, 2018

Thanks, but in ViewDidAppear it is nil

... pagingViewController.select(pagingItem: CalendarItem(date: Date())) if (pagingViewController.pageViewController.selectedViewController == nil) { print("NIL") }

I need to replicate this on start:

`
func pagingViewController(_ pagingViewController: PagingViewController, didScrollToItem pagingItem: T, startingViewController: UIViewController?, destinationViewController: UIViewController, transitionSuccessful: Bool) where T : PagingItem, T : Comparable, T : Hashable {

    currentController = destinationViewController as! CollectionViewController

}
}
`

Thanks a lot

@mimmo00
Copy link
Author

mimmo00 commented Aug 25, 2018

@rechsteiner maybe the problem is that I'm using a subclass of PagingViewController?

class ResizedPagingViewController : PagingViewController<CalendarItem> { override func loadView() { view = ResizedPagingView( options: options, collectionView: collectionView, pageView: pageViewController.view) } }

@rechsteiner
Copy link
Owner

Using a subclass shouldn't be a problem. The view controller is not added before viewDidAppear. Are you sure you are checking there and not in viewDidLoad? I just tried adding this to the CalendarExample and it logs the selected view controller just fine:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    print(pagingViewController.pageViewController.selectedViewController)
  }
Optional(<CalendarExample.CalendarViewController: 0x7fb74a42fc00>)

@mimmo00
Copy link
Author

mimmo00 commented Aug 26, 2018

Yes, I tried everything inside viewDidAppear
For now I solved this dirty way (inside viewDidAppear ):

DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {

                self.pagingViewController.select(pagingItem: CalendarItem(date: Date()))

            })

Thanks!

@rechsteiner
Copy link
Owner

Can you try calling select(pagingItem:) in viewDidLoad then printing selectedViewController in viewDidAppear?

@mimmo00
Copy link
Author

mimmo00 commented Aug 26, 2018

Yes, I tried again now. If I'm logged in, it crash because it loads the listing from server. If I am not logged in, the empty status appears without problems. Probably it is some strange logic of the listing to cause the error, not the library :)

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

2 participants