1

So I am using this Github package: https://github.com/jakespracher/Snapchat-Swipe-View

And I have a problem. viewDidLoad and viewDidAppear does only run once, when the application has loaded. How can I run a code every time the let left = storyboard.instantiateViewControllerWithIdentifier("left") appears on the screen?

Roduck Nickes
  • 1,021
  • 2
  • 15
  • 41
  • 1
    Could you please clarify what you mean about run code every time you instantiate a new view controller. If you just want to run code every time the view is shown to the user, then maybe viewWillAppear or viewDidAppear is what you're after? – Welton122 May 30 '16 at 11:51
  • `viewDidload` run only once when view is initialized. and `viewdidAppear` run every time after view is appeared. – Ketan Parmar May 30 '16 at 12:03
  • @Welton122 As I mentioned - `viewDidAppear` only run once when using this Github package. If you download the Github pack and test it out, you will see that it doesn't run more then once. – Roduck Nickes May 30 '16 at 12:09

3 Answers3

1

I believe the viewWillAppear method will still run every time. Otherwise you could use viewDidLayoutSubviews.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
  • Both of them only works when the application has loaded, but they do not run if I change to the `UIViewController` that the code is inside. I tried to add a `println("Visible")`, it shows in the log window only when application is loaded, but not when changing back and forward the `UIViewControllers`. – Roduck Nickes May 30 '16 at 12:29
  • @RoduckNickes what about `viewWillLayoutSubview`? By the way, there is already an issue about this on Github. Hopefully it will get fixed soon. – Pranav Wadhwa May 30 '16 at 12:32
  • I get the same issue for `viewWillLayoutSubview`. Yes, I see.. I hope it get fixed! – Roduck Nickes May 30 '16 at 12:39
  • If you have time, please also take a look at this question I have: http://stackoverflow.com/questions/37505153/parse-query-after-a-specific-row/ – Roduck Nickes May 30 '16 at 12:44
  • @RoduckNickes I answered your other question as well. You can take a look at it. – Pranav Wadhwa May 30 '16 at 12:57
1

Snapchat-Swipe-View loads all of the viewControllers and views into scrollView of SnapContainerViewController at once.

All of the viewControllers are childViewController of SnapContainerViewController.

therefore

  1. all viewControllers will received viewDidLoad and viewDidAppear at once

  2. technically all viewControllers are already at "appear" state, it won't call "viewDidAppear" again before "viewDidDisappear"

  3. paging change(switching view to on-screen) can be identified from scrolling event.

Example to identify which viewController is on-screen (for horizontal scrollview with index 0, 1, 2) Similar handling for vertical scrollView.

Simply add the following callback method in SnapContainerViewController.swift

extension SnapContainerViewController: UIScrollViewDelegate {
   func scrollViewDidEndDecelerating(scrollView: UIScrollView){
        let fullWidth = scrollView.frame.size.width
        let pageNo: NSInteger = lround(Double(scrollView.contentOffset.x / fullWidth))

        print("page is shown:", pageNo)
    }
Codebear
  • 196
  • 4
  • Yea, that worked! On page number 3 I have a class connected: Home.swift how do I set in that controller a code to run every time the view controller is displayed? – Roduck Nickes Jun 07 '16 at 19:03
0

Like you say, each ViewController is loaded when the app is loaded, the view is magnified and you only scroll between the View Controllers. Therefore, you are missing the benefits of UIViewController class. Your best bet is:

  1. To get the screen location by setting property listeners to the center of the UIScrollView, see Understanding UIScrollView.
  2. Set timer for the change or recognize touch.
  3. Use a better project, and modify it to your needs
Community
  • 1
  • 1
Idan
  • 5,405
  • 7
  • 35
  • 52