我想将一个对象从ViewA传递给ViewB,但ViewB嵌入在一个导航控制器中,该控制器嵌入在一个标签栏控制器中,该控制器是一个容器视图的视图。
我在prepareforsegue中尝试了这个,但是值没有传递到目的地。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"OpenProjectsSegue"]) {
ContainerViewController *cvc = segue.destinationViewController;
UITabBarController *tbc = cvc.tabBarController;
UINavigationController *nav = tbc.viewControllers[0];
IncomeViewController *vc = (IncomeViewController *)nav.topViewController;
vc.projectIndexToOpen = @1;
vc.testString = @"hola peru";
}
}
我也试过了UITabBarController *tbc = cvc.childviewcontroller[0]
,但得到了一个空数组错误消息。
请帮忙。谢谢!
答案 0 :(得分:0)
我有一个可能有用的答案。
顶部的ViewController.h中的写:
extern UITabBarController *tbc;
然后,在顶部的ViewController.m中写:
UITabBarController *tbc;
此外,您必须在您尝试访问UITabBarController的任何文件中#include您的类,但这将使UITabBarController成为一个全局变量,您可以在多个文件中访问它。
答案 1 :(得分:0)
问题在于时机问题。在prepareForSegue中,ContainerViewController的viewControllers属性为空,因为所有其他控制器尚未初始化或作为子项添加。我做了与你相同的设置,当我记录他们生命周期中的各个阶段时,这就是我得到的,
2014-04-21 17:01:23.930 PassingAheadInComplexHierarchyProblem[13653:60b] In ContainerViewController init
2014-04-21 17:01:23.931 PassingAheadInComplexHierarchyProblem[13653:60b] In prepareForSegue in ProjectTypeViewController
2014-04-21 17:01:23.931 PassingAheadInComplexHierarchyProblem[13653:60b] ContainerViewController's children are (logged from prepareForSegue): (
)
2014-04-21 17:01:23.934 PassingAheadInComplexHierarchyProblem[13653:60b] In IncomeViewController init
2014-04-21 17:01:23.935 PassingAheadInComplexHierarchyProblem[13653:60b] In NavController init
2014-04-21 17:01:23.936 PassingAheadInComplexHierarchyProblem[13653:60b] In TabBarController init
2014-04-21 17:01:23.937 PassingAheadInComplexHierarchyProblem[13653:60b] In TabBarController viewDidLoad
2014-04-21 17:01:23.937 PassingAheadInComplexHierarchyProblem[13653:60b] In TabBarController didMoveToParentVC: <ContainerViewController: 0x8c227c0>
2014-04-21 17:01:23.937 PassingAheadInComplexHierarchyProblem[13653:60b] In ContainerViewController viewDidLoad
2014-04-21 17:01:23.940 PassingAheadInComplexHierarchyProblem[13653:60b] In NavController viewDidLoad
2014-04-21 17:01:23.948 PassingAheadInComplexHierarchyProblem[13653:60b] In IncomeViewController viewDidLoad
2014-04-21 17:01:24.452 PassingAheadInComplexHierarchyProblem[13653:60b] In IncomeViewController didMoveToParentVC: <NavController: 0x8e4f860>
如您所见,在调用prepareForSegue时,只有ContainerViewController被初始化。
在IncomeViewController中获取所需数据的一种方法是获取对ProjectTypeViewController的引用,并查询其属性。该引用很容易获得,因为ProjectTypeViewController将是IncomeViewController的presentsViewController。因此,如果你在两个名为projectIndexToOpen的控制器中都有属性,你可以在IncomeViewController中执行此操作,
- (void)viewDidLoad {
[super viewDidLoad];
self.projectIndexToOpen = [(ProjectTypeViewController *)self.presentingViewController projectIndexToOpen];
NSLog(@"%@",self.projectIndexToOpen);
}