从UITabBar初始化UIViewController的属性

时间:2014-11-05 15:47:15

标签: ios uiviewcontroller uitabbar

我需要在TabBar中单击时设置ViewController的属性。

我尝试在didSelectViewController中执行此操作 - 但在ViewWillAppear完成后调用它并且为时已晚

我尝试过这样做:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

但是viewController是原始tabBarIndex,而不是目标tabBarIndex

我该怎么办?

2 个答案:

答案 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;
    }
}