[英]Update all view controllers of a UITabBarController
I have a UITabBarController
with four tabs. 我有一个带有四个选项卡的
UITabBarController
。 In each of the view controllers presented when a tab is selected I have a reset button. 在选择选项卡时显示的每个视图控制器中,我都有一个重置按钮。 Tapping the button will change the appearance of all the view controllers.
点击按钮将改变所有视图控制器的外观。 In particular, it will change the text of some labels in the different view controllers.
特别是,它将更改不同视图控制器中某些标签的文本。
Is there some recommended way to update all the view controllers of a UITabBarController
at the same time ie to make them reload their views? 是否有一些推荐的方法同时更新
UITabBarController
所有视图控制器,即让他们重新加载他们的视图?
My current approach is to make those view controllers conform to a protocol 我目前的方法是使这些视图控制器符合协议
@protocol XYReloadableViewController
- (void)reloadContents;
@end
and then send the message -reloadContents
to all the view controllers when the button is tapped: 然后在点击按钮时将消息
-reloadContents
发送到所有视图控制器:
- (IBAction)touchUpInsideResetButton {
// ...
NSArray *viewControllers = self.tabBarController.viewControllers;
for (UIViewController<XYReloadableViewController> *viewController in viewControllers) {
[viewController reloadContents];
}
}
Then in each of the view controllers I would have to implement that method: 然后在每个视图控制器中,我将不得不实现该方法:
- (void)reloadContents {
[self.tableView reloadData];
// other updates to UI ...
}
But that seems a little too complicated. 但这似乎有点过于复杂。 So is there an easier way to tell the view controllers to reload their views?
那么是否有更简单的方法告诉视图控制器重新加载他们的视图?
Edit: And what happens if I present a UINavigationController
in some of the tabs or a container view controller? 编辑:如果我在某些选项卡或容器视图控制器中提供
UINavigationController
会发生什么? I would need to pass the message along the chain of all its child view controllers... 我需要沿着所有子视图控制器的链传递消息...
You can create ReloadViewController and all you contrlollers inheritance from him. 您可以创建ReloadViewController以及他所有的contrlollers继承。
ReloadViewController have property UIButton and methods: ReloadViewController具有属性UIButton和方法:
in .m file: 在.m文件中:
-(void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadContents)
name:@"MyNotification"
object:nil];
}
- (IBAction)touchUpInsideResetButton:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
object:nil];
}
in your viewControllers need only override method reloadContents 在您的viewControllers中只需要覆盖方法reloadContents
Notifications sound like a better fit for this. 通知听起来更适合这种情况。 When view controllers need to be reset, broadcast an
NSNotification
and have any view controllers that might need to reset themselves listen for that notification, and trigger what they need to do. 当需要重置视图控制器时,广播
NSNotification
并让任何可能需要重置的视图控制器监听该通知,并触发他们需要执行的操作。 That way it doesn't matter how far down a navigation stack they are. 这样,它们在导航堆栈的下方距离无关紧要。
You might want to defer updates until the view actually appears. 您可能希望推迟更新,直到视图实际显示为止。 You could set a
BOOL
needsUpdate
when the VCs receive the notification, but only do the actual update in viewWillAppear:
, to save resources and prevent a large number of updates from going off at once (and perhaps blocking the main thread). 您可以在VC收到通知时设置
BOOL
needsUpdate
,但只在viewWillAppear:
执行实际更新,以节省资源并防止大量更新立即关闭(并且可能阻止主线程)。
If this behaviour is common to all your view controllers, make a UIViewController
subclass to prevent repeating code and have them all inherit from that. 如果这种行为对于所有视图控制器都是通用的,那么创建一个
UIViewController
子类以防止重复代码并让它们全部继承。 Alternatively, (if you're using Apple VC subclasses) make a category on UIViewController
to add the notification methods. 或者,(如果您正在使用Apple VC子类)在
UIViewController
上创建一个类别以添加通知方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.