我需要在TabBar中单击时设置ViewController的属性。
我尝试在didSelectViewController
中执行此操作 - 但在ViewWillAppear
完成后调用它并且为时已晚
我尝试过这样做:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
但是viewController
是原始tabBarIndex,而不是目标tabBarIndex
我该怎么办?
答案 0 :(得分:0)
在挖掘代码之后,我找到了一种方法:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
UINavigationController *nav = (UINavigationController *)viewController;
if ([[nav.viewControllers objectAtIndex:0] isKindOfClass:[DestinationViewController class]])
{
DestinationViewController *vc = (DestinationViewController *)[nav.viewControllers objectAtIndex:0];
vc.foo = 1;
}
return YES;
}
答案 1 :(得分:0)
如果您使用的是故事板,则可以设置segue ID,然后:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:YOUR_SEGUE_ID]) {
YourViewController *controller = segue.destinationViewController;
controller.someProperty = someProperty;
}
}