在UITabBarController中切换VC时使用prepareForSegue:Sender
Use prepareForSegue:Sender when switching VC in UITabBarController
我用谷歌搜索了很多,但一切都非常混乱。我需要它,所以当标签栏切换到其他视图控制器时,当视图控制器即将切换时,是否会调用一种方法,获取目标控制器并设置一些变量以在MKMAPVIEW中添加一些注释。我该怎么做?
有很多选择,但其中之一是实现 UITabBarControllerDelegate
协议,然后将实现它的 class 设置为您的 UITabBarController
的委托。代表收到消息- tabBarController:didSelectViewController:
在该方法中,您可以通过查看最后一个视图控制器和下一个视图控制器来实现您想要的行为。要从您的委托访问这些视图控制器,您可能需要将它们作为 weak
属性添加到您的委托 class。您还可以通过其 viewControllers
属性 访问所有 UITabBarController
的子视图控制器,这是一个视图控制器数组。
在对委托方法进行一些试验后,我找到了答案。
我用的是- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
方法。
这感觉像是一个相当不错的替代品,因为您可以轻松获得每个控制器。
警告:此方法不应用于处理大块代码,但要将视图控制器设置为运行大块代码。
一个测试实现:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
FirstViewController *currentController = (FirstViewController *)[tabBarController selectedViewController];
SecondViewController *destinationController = (SecondViewController *)viewController;
// If you want, do some code on these here. For more precision, read on.
return YES;
}
这可以与逻辑一起使用来确定您是否应该执行某些特定代码:EX:
if ([[tabBarController selectedViewController] class] == [FirstViewController class]) {
if ([viewController class] == [SecondViewController class]) {
// It is going from first to second. Do some code here.
}
}
此外,您必须设置委托
TabBarController .h
@interface TabBarController : UITabBarController<UITabBarControllerDelegate>
...
TabBarController .m
...
- (void)viewDidLoad
{
self.delegate = self;
}
...
我用谷歌搜索了很多,但一切都非常混乱。我需要它,所以当标签栏切换到其他视图控制器时,当视图控制器即将切换时,是否会调用一种方法,获取目标控制器并设置一些变量以在MKMAPVIEW中添加一些注释。我该怎么做?
有很多选择,但其中之一是实现 UITabBarControllerDelegate
协议,然后将实现它的 class 设置为您的 UITabBarController
的委托。代表收到消息- tabBarController:didSelectViewController:
在该方法中,您可以通过查看最后一个视图控制器和下一个视图控制器来实现您想要的行为。要从您的委托访问这些视图控制器,您可能需要将它们作为 weak
属性添加到您的委托 class。您还可以通过其 viewControllers
属性 访问所有 UITabBarController
的子视图控制器,这是一个视图控制器数组。
在对委托方法进行一些试验后,我找到了答案。
我用的是- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
方法。
这感觉像是一个相当不错的替代品,因为您可以轻松获得每个控制器。
警告:此方法不应用于处理大块代码,但要将视图控制器设置为运行大块代码。
一个测试实现:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
FirstViewController *currentController = (FirstViewController *)[tabBarController selectedViewController];
SecondViewController *destinationController = (SecondViewController *)viewController;
// If you want, do some code on these here. For more precision, read on.
return YES;
}
这可以与逻辑一起使用来确定您是否应该执行某些特定代码:EX:
if ([[tabBarController selectedViewController] class] == [FirstViewController class]) {
if ([viewController class] == [SecondViewController class]) {
// It is going from first to second. Do some code here.
}
}
此外,您必须设置委托
TabBarController .h
@interface TabBarController : UITabBarController<UITabBarControllerDelegate>
...
TabBarController .m
...
- (void)viewDidLoad
{
self.delegate = self;
}
...