prepareForSegue和代表 [英] prepareForSegue and delegates
问题描述
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@MoreOptions]){
UINavigationController * navigationController = segue.destinationViewController;
MoreOptionsViewController * controller =(MoreOptionsViewController *)navigationController.topViewController;
controller.delegate = self;
} else if([segue.identifier isEqualToString:@FullStoryView]){
SingleStoryViewController * detailViewController = segue.destinationViewController;
detailViewController.urlObject = sender;
}
}
所有这一切都正常,但我想尝试更好地了解代码。我不明白的是,我必须通过从navigationController.topViewController获取参考,而不是像在第二个if条件中那样从segue.destinationViewController中获取它,从而获得对MoreOptionsViewController的引用。是因为我将当前视图控制器(self)设置为代理?再次,我不是想解决问题,只是想更好地了解发生了什么。
看看你的故事板,应该明白为什么会这样。您在 UINavigationController
中嵌入了 MoreOptionsViewController
,并将其连接到导航控制器,从而使其成为 destinationViewController
。这是很常见的。
I have an application with two segues. In one of the segues, the current view controller becomes a delegate and the other does not.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"MoreOptions"]) {
UINavigationController *navigationController = segue.destinationViewController;
MoreOptionsViewController *controller = (MoreOptionsViewController *)navigationController.topViewController;
controller.delegate = self;
} else if ([segue.identifier isEqualToString:@"FullStoryView"]) {
SingleStoryViewController *detailViewController = segue.destinationViewController;
detailViewController.urlObject = sender;
}
}
All of this is working fine, but I would like to try and understand the code better. What I don't understand is that I have to get a reference to the MoreOptionsViewController by grabbing it from navigationController.topViewController rather than simply getting it from segue.destinationViewController like I do in the second if condition. Is it because I'm setting the current view controller (self) as the delegate? Again, I'm not trying to solve a problem, just trying to get a better understanding of what's going on.
Take a look at your storyboard and it should be evident why this is the case. You have embedded MoreOptionsViewController
in a UINavigationController
and connected a segue to the navigation controller, thus making it the destinationViewController
. This is fairly common.
这篇关于prepareForSegue和代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!