Ios 从UITabBar初始化UIViewController的属性

Ios 从UITabBar初始化UIViewController的属性,ios,uiviewcontroller,uitabbar,Ios,Uiviewcontroller,Uitabbar,我需要在从选项卡栏单击时设置ViewController的属性 我尝试在didSelectViewController中执行此操作,但在viewwillbeen完成后调用此操作,为时已晚 我试着在以下方面做: - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 但是viewController是源

我需要在从选项卡栏单击时设置ViewController的属性

我尝试在
didSelectViewController
中执行此操作,但在
viewwillbeen
完成后调用此操作,为时已晚

我试着在以下方面做:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
但是
viewController
是源tabBarIndex,而不是目标tabBarIndex


我能做什么

在深入研究代码之后,我找到了一种方法:

- (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;
}

如果您使用的是故事板,则可以设置segue ID,然后:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:YOUR_SEGUE_ID]) {
        YourViewController *controller = segue.destinationViewController;
        controller.someProperty = someProperty;
    }
}

是的,
shoulldselect…
是正确的选择。