导航控制器和容器视图之间的通信
Communication between navigation controller and container view
Parent
View Controller
_________________
| |
| |
| | Navigation Cont.
|_________________| _________________
| | | |
| | | |
| Container View | --> | | --> Child View Controllers
| | | |
|_________________| |_________________|
我的应用有一个导航结构,由一个父视图控制器和一个包含导航控制器(及其子视图控制器)的容器组成。问题是在这些子视图控制器的初始化过程中,我想传递父视图控制器提供的一些值。不幸的是,父级中使用的方法 prepareForSegue
仅检测容器 segue,但在子视图控制器更改时不会调用它。
如何解决这个问题?我正在使用 Swift 但我也理解 Objective-C.
您可以使用prepareforsegue
,例如
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
您可以从容器中执行此操作 viewcontroller
NSArray *viewControllers = self.navigationController.viewControllers;
UIViewController *rootViewController = (UIViewController *)[viewControllers objectAtIndex:viewControllers.count - 2]; //index of your root viewcontroller
并参考 this link 将数据传递给“containerview 的 VC”
好吧,最后花了点时间...但我终于明白了。
很简单:child 将通过 self.parentview.parentview
与 parent 对话。几个代表就完成了。
Parent
View Controller
_________________
| |
| |
| | Navigation Cont.
|_________________| _________________
| | | |
| | | |
| Container View | --> | | --> Child View Controllers
| | | |
|_________________| |_________________|
我的应用有一个导航结构,由一个父视图控制器和一个包含导航控制器(及其子视图控制器)的容器组成。问题是在这些子视图控制器的初始化过程中,我想传递父视图控制器提供的一些值。不幸的是,父级中使用的方法 prepareForSegue
仅检测容器 segue,但在子视图控制器更改时不会调用它。
如何解决这个问题?我正在使用 Swift 但我也理解 Objective-C.
您可以使用prepareforsegue
,例如
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
您可以从容器中执行此操作 viewcontroller
NSArray *viewControllers = self.navigationController.viewControllers;
UIViewController *rootViewController = (UIViewController *)[viewControllers objectAtIndex:viewControllers.count - 2]; //index of your root viewcontroller
并参考 this link 将数据传递给“containerview 的 VC”
好吧,最后花了点时间...但我终于明白了。
很简单:child 将通过 self.parentview.parentview
与 parent 对话。几个代表就完成了。