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

viewDidLoad only run once #2

Open
ghost opened this issue May 29, 2016 · 10 comments
Open

viewDidLoad only run once #2

ghost opened this issue May 29, 2016 · 10 comments
Assignees

Comments

@ghost
Copy link

ghost commented May 29, 2016

@jakespracher I noticed that the viewDidLoad only run once, even if I swipe to open the same view controller multiple times.. Why is that?

@jakespracher
Copy link
Owner

All of the scroll views here are instantiated and rendered at the same time even if the others are out of view. If you open the view debugger and click the button to show clipped view you will see this. Because of this, viewDidLoad will only be called once. This is very difficult to avoid with this type of UI

@ghost
Copy link
Author

ghost commented Jun 7, 2016

@jakespracher Hm, I see.. Any ideas how I can run code every time one of the view controllers shows up?

@devJoshLopez
Copy link

devJoshLopez commented Aug 12, 2016

I love this but I am having a really hard time trying to figure out how to run a function when the viewcontroller is seen. Has anyone figured this out yet? Even viewwillappear i being called when the app starts and doesnt fire again.

@otymartin
Copy link

@devJoshLopez Did you figure it out? Almost every tutorial or library with this navigation style omits addressing this key tradeoff.

@jakespracher
Copy link
Owner

Hmm, for whatever reason I didn't need this type of thing for the app that inspired this. I'll try to work on this if I have time

@jakespracher jakespracher self-assigned this Oct 18, 2016
@zalsaeed
Copy link

Responding to the question of how one could know which view is being loaded at the moment? I think this could only be found by monitoring the scrollView.contentOffset for x and y axis. I haven't implemented this, but I think it is possible since we know the Offset for each view (middle, top, bottom, left and right) and they will never change. The only thing that I haven't checked is where exactly should such check/monitoring be placed. My initial thought is that the method scrollViewWillBeginDragging would be the place, but not sure. I hope this helps.

@otymartin
Copy link

@zalsaeed the point is that we need those viewControllerLifeCycle methods eg viewDidLoad, viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear to write custom code and execute custom behaviour on a specific viewController before and after it loads.

@zalsaeed
Copy link

@otymartin I think I understand the goal, but since the way this idea (Snapchat like navigation) is implemented doesn't allow the usage of those handy methods I was trying to find a walk around. The main goal is to know which view is shown at the moment (for me I want to trigger some methods as soon as a view is active), using the coordinates value is a possible solution in my opinion to that specific goal. The solution I'm proposing is definitely not functionally as good as the provided viewControllerLifeCycle methods since there is no way to capture the transition stages (e.g. willDisappear and willAppear). Please note that I'm new to iOS/Swift application, so I'm not claiming that this is the most elegant solution, but I can't think of another one at the moment. Have you found any way to know which view is being loaded even if it doesn't use the viewControllerLifeCycle methods?

@otymartin
Copy link

otymartin commented Dec 27, 2016

@zalsaeed somehow EZSwipeController managed to do just that. perhaps thier implementation can be looked at. its what i ended up using

@petard
Copy link

petard commented Sep 21, 2017

if added this as a work-around to notify views when they appear:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // tell child views they have appeared / disappeared
        for vc in views {
            if scrollView.bounds.intersection(vc.view.frame) == vc.view.frame {
                vc.viewDidAppear(false)
                previousVC?.viewDidDisappear(false)
                previousVC = vc
            }
        }
    }

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

5 participants