简体   繁体   中英

Page View Controller update view controllers ?

I have a page view controller and each time the user swipes I use this method to detect if the transition completed.

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed

Now I have 5 view controllers that I display in my page view controller, and each of those view controllers have a UILabel on them. Using the method above to detect a successful transition, I want to update the UILabels with data from the page view controller class each time the transition completes.

So every time the user swipes I want the UILabels on the 5 view controllers to be updated with new values from my page view controller class.

What is the best way to do this, (regularly updating strings/calling methods from a different class)? I have looked around and I couldn't find anything about this?! Any help would be greatly appreciated, thanks.

You can do it in two ways,

  1. If you are creating UILabel programmatically to have all your sub view controllers inherit from a view controller which has a UILabel so the code would be something like this

     @interface ViewControllerWithLabel : UIViewController @property (strong, nonatomic) UILabel *someLabel; @end 

    and then all your controllers which are in the pages will be inherited from this like

     @interface ViewControllerPage1: ViewControllerWithLabel 

    ...

     @interface ViewControllerPage2: ViewControllerWithLabel 

    ...

     @interface ViewControllerPage3: ViewControllerWithLabel 

    ...

     @interface ViewControllerPage4: ViewControllerWithLabel 

    ...

     @interface ViewControllerPage5: ViewControllerWithLabel 

and in your

    - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
    {
        for(ViewControllerWithLabel *changeView in pageViewController.viewControllers)
        {
             changeView.someLabel = @"Some Text";
        }
     }
  1. Second way if you are using Storyboard and IBOutlet you will have to check each type of ViewController and set the label as follows

      - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed { for(UIViewController *changeView in pageViewController.viewControllers) { if ([changeView isKindOfClass:[ViewControllerPage1 class]] { ViewControllerPage1* temp = (ViewControllerPage1*)changeView; temp.labelName.text = @"Some Text"; } else if ([changeView isKindOfClass:[ViewControllerPage2 class]] { ViewControllerPage2* temp = (ViewControllerPage2*)changeView; temp.labelName.text = @"Some Text"; } else if ([changeView isKindOfClass:[ViewControllerPage3 class]] { ViewControllerPage3* temp = (ViewControllerPage3*)changeView; temp.labelName.text = @"Some Text"; } else if ([changeView isKindOfClass:[ViewControllerPage4 class]] { ViewControllerPage4* temp = (ViewControllerPage4*)changeView; temp.labelName.text = @"Some Text"; } else if ([changeView isKindOfClass:[ViewControllerPage5 class]] { ViewControllerPage5* temp = (ViewControllerPage5*)changeView; temp.labelName.text = @"Some Text"; } } } 

So every time the user swipes I want the UILabels on the 5 view controllers to be updated with new values from my page view controller class.

You can't, for the simple reason that "the 5 view controllers" do not exist. That is not how a UIPageViewController works. It has just one child view controller at a time — the current page.

So, you can update the label for a particular view controller when the user scrolls to that view controller (page). At that moment, you will have to create and supply the view controller afresh, and you can configure it with the appropriate value of the label. Otherwise, you just have to sit there, storing the value that the label will have, until that moment comes.

The best way I found to do this guys is through NSNotifications. I create an observer in each of my view controller classes I want to update, then I simply call the Notifications in the main class and the view controllers then call a method inside them! So simple and clean!

Add this in the class you want to be updated:

 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateLabel:)
                                                 name:@"LABELUPDATENOTIFICATION1"
                                               object:nil];

Create a method for it:

- (void)updateLabel:(NSNotification*)notification
{
    NSString *updatedText = (NSString*)[notification object];
    [nameLabel setText:updatedText];
}

Then call this from any other class and the method you created will be called:

[[NSNotificationCenter defaultCenter] postNotificationName:@"LABELUPDATENOTIFICATION1"object:content1];

I am very surprised no one suggested this to me from the beginning, as this is so simple, all the other answers seemed so complex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM