Ios 页面视图控制器是否更新视图控制器?
我有一个页面视图控制器,每次用户滑动时,我都使用此方法来检测转换是否完成Ios 页面视图控制器是否更新视图控制器?,ios,objective-c,methods,updating,Ios,Objective C,Methods,Updating,我有一个页面视图控制器,每次用户滑动时,我都使用此方法来检测转换是否完成 - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed 现在,我在页面视图控制器中
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
现在,我在页面视图控制器中显示了5个视图控制器,每个视图控制器上都有一个UILabel。使用上面的方法来检测成功的转换,我想在每次转换完成时用page view controller类中的数据更新UILabel。
因此,每次用户滑动时,我都希望5个视图控制器上的UILabel更新为页面视图控制器类中的新值
最好的方法是什么(定期更新字符串/从其他类调用方法)?我到处找了找,却找不到任何关于这个的东西?!任何帮助都将不胜感激,谢谢。您可以通过两种方式来实现
UILabel
以使所有子视图控制器从具有UILabel
的视图控制器继承,则代码如下所示
@interface ViewControllerWithLabel : UIViewController
@property (strong, nonatomic) UILabel *someLabel;
@end
然后,页面中的所有控制器都将继承自以下内容
@interface ViewControllerPage1: ViewControllerWithLabel
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
for(ViewControllerWithLabel *changeView in pageViewController.viewControllers)
{
changeView.someLabel = @"Some Text";
}
}
- (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";
}
}
}
因此,当用户滚动到某个视图控制器(第页)时,可以更新该视图控制器的标签。此时,您必须重新创建并提供视图控制器,并且可以使用适当的标签值对其进行配置。否则,你只需要坐在那里,存储标签将拥有的价值,直到那一刻到来。我发现最好的方法就是通过通知。我在我想要更新的每个视图控制器类中创建一个观察者,然后我只需调用主类中的通知,视图控制器然后调用其中的一个方法!如此简单和干净 将其添加到要更新的类中:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateLabel:)
name:@"LABELUPDATENOTIFICATION1"
object:nil];
为它创建一个方法:
- (void)updateLabel:(NSNotification*)notification
{
NSString *updatedText = (NSString*)[notification object];
[nameLabel setText:updatedText];
}
然后从任何其他类调用此函数,将调用您创建的方法:
[[NSNotificationCenter defaultCenter] postNotificationName:@"LABELUPDATENOTIFICATION1"object:content1];
我很惊讶从一开始就没有人向我提出这一点,因为这是如此简单,所有其他答案似乎如此复杂 对,你确定没有别的办法吗?因为我使用的是我不想等待加载的照片,所以我希望它们预加载?您可以预加载它们。但是在构成页面的视图控制器中没有。感谢您提供了这样一个深入的答案,唯一的困惑是“ClassName”是什么,以及“ViewControllerPageNumber”是什么?我尝试了各种变量,但总是出错?
- (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";
}
}
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateLabel:)
name:@"LABELUPDATENOTIFICATION1"
object:nil];
- (void)updateLabel:(NSNotification*)notification
{
NSString *updatedText = (NSString*)[notification object];
[nameLabel setText:updatedText];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"LABELUPDATENOTIFICATION1"object:content1];