简体   繁体   English

页面视图控制器更新视图控制器?

[英]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. 现在,我有5个视图控制器显示在页面视图控制器中,并且每个视图控制器上都有一个UILabel 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. 使用以上方法检测成功的过渡,我想在每次过渡完成时使用页面视图控制器类中的数据更新UILabel。

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. 因此,每次用户滑动时,我都希望使用页面视图控制器类中的新值来更新5个视图控制器上的UILabel。

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 如果要以编程方式创建UILabel以使所有子视图控制器都从具有UILabel视图控制器继承,那么代码将是这样的

     @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 第二种方法,如果您使用Storyboard和IBOutlet,则必须检查每种类型的ViewController并按如下所示设置标签

      - (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. 因此,每次用户滑动时,我都希望使用页面视图控制器类中的新值来更新5个视图控制器上的UILabel。

You can't, for the simple reason that "the 5 view controllers" do not exist. 您不能这样做,原因很简单,即不存在“ 5个视图控制器”。 That is not how a UIPageViewController works. 这不是UIPageViewController的工作方式。 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. 我发现做到这一点的最好方法是通过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! 我在要更新的每个视图控制器类中都创建了一个观察者,然后只需在主类中调用Notifications,然后在视图控制器中调用其中的方法! 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. 我很惊讶没有人从一开始就建议我这样做,因为这很简单,所有其他答案似乎都那么复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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