0

There are two view controllers in my app, e.g, vc1 and vc2. The two view controllers are as the subviews of a scrollView, so the user can scroll the screen to switch the view. However, the simple implement has a problem: the viewWillAppear method of vc1 and vc2 is called only once. so I want to implement my scroll container view controller, which can call viewWillAppear method correctly, please tell me how to implement it.

4

3 回答 3

1

I am not sure what you are trying to do, but I think a simple UITableView or UICollectionView may be better for you because they have datasource method that will automatically called when a view will show up in the screen. You can update your two views when you need to return a UITableViewCell or UICollectionViewCell.

于 2013-08-04T08:33:59.967 回答
0

I'm not sure if this will work, but I'm thinking you can check if the vc1 and vc2's frames are withing the screen's bounds in the delegate method of the scrollView.

I'm pretty sure there's a method being called every time the scrollView is being scrolled. In this method, you can check

//put this in your .h or something
BOOL vc1IsVisible = true;

//in the scrollView delegate-method that is called upon scrolling
if([self isInsideView:vc1])
{
    if(!vc1IsVisible)
    {
        vc1IsVisible = true;
        [vc1 viewDidAppear:NO]; //or whatever it is for animation
    }
}
else
{
    if(vc1IsVisible)
        vc1IsVisible = false
        //and viewDidDisappear?
}

and then create a method somewhere like this

-(BOOL)isInsideView:(UIViewController*)vc
{
    //Check if vc.origin.y is greater than scrollView.size.height or something maybe?
    //You can probably also try using the scrollView's contentOffset and use that
    //relative to the viewController's sizes.
    //if the viewControllers bounds are withing the scrolls bounds, return YES;
    //else, return NO;
}

Sorry I can't really test anything just now. Maybe I'll make something and update the answer later if you haven't figured it out. And you need to do it with both. I'm sure you can figure out a better way to include both in one method with this, or even with one variable.

于 2013-08-03T17:41:58.390 回答
0

Since you are using ViewController by adding it subview of scrollview, by adding ViewController this way viewDidLoad, viewWillAppear, viewDidAppear will be called only once, I mean there is no use of viewWillAppear here as such, rather if you want to update anything in the added ViewController you should create a public class in ViewController and call it when you need an update..

于 2013-08-03T17:48:09.123 回答