ios 使用包含另一个视图的导航控制器在 segue 中设置属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19774583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting a property in a segue with Navigation Controller containing another view
提问by Kamaros
I'm getting a crash when trying to set a property in my segue. It's a UIView passing a property to a Navigation Controller that has a TableView as it's root view. It's supposed to go to my TableViewController, but it looks like it is getting intercepted by that NavigationController and throwing an unrecognized selector error.
尝试在我的 segue 中设置属性时发生崩溃。它是一个 UIView 将属性传递给具有 TableView 作为根视图的导航控制器。它应该转到我的 TableViewController,但看起来它被那个 NavigationController 拦截并抛出一个无法识别的选择器错误。
Segue:
赛格:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showItems"]) {
ShowItemsTableViewController *destinationViewController = [segue destinationViewController];
[destinationViewController setItems:[self itemsFromCoreData]];
}
}
Error:
错误:
-[UINavigationController setItems:]: unrecognized selector sent to instance 0x10920c840
-[UINavigationController setItems:]: unrecognized selector sent to instance 0x10920c840
What's going on here? That NavigationController doesn't have a class associated with it, it's just in the storyboard and comes up modally. It works if I set the segue in the storyboard to go directly to the view, not the NavigationController, but I really need the nav there. How do I get around this?
这里发生了什么?NavigationController 没有与之关联的类,它只是在情节提要中并以模态方式出现。如果我将故事板中的 segue 设置为直接转到视图,而不是 NavigationController,它会起作用,但我确实需要那里的导航。我该如何解决这个问题?
回答by Kamaros
Since the destination view controller is actually the navigation controller, try accessing the root view like so:
由于目标视图控制器实际上是导航控制器,请尝试像这样访问根视图:
UINavigationController *navController = [segue destinationViewController];
ShowItemsTableViewController *SITViewController = (ShowItemsTableViewController *)([navController viewControllers][0]);
[SITViewController setItems:[self itemsFromCoreData]];
回答by Sti?o
For Swift:
对于斯威夫特:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueShowNavigation" {
var DestViewController = segue.destinationViewController as! UINavigationController
let targetController = DestViewController.topViewController as! ReceiveViewController
}
}
回答by Guto Araujo
Get the topViewController from the UINavigationController:
从 UINavigationController 获取 topViewController:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showItems"]) {
UINavigationController *navigationController = segue.destinationViewController;
ShowItemsTableViewController *showItemsTVC = (ShowItemsTableViewController * )navigationController.topViewController;
showItemsTVC.items = [self itemsFromCoreData];
}
}
回答by David H
Look at the view controller class in the prepare for segue - it's the nav controller. Cast it to that class, then you can get to the desired view by asking it for its top view controller.
查看准备转场中的视图控制器类 - 它是导航控制器。将它投射到那个类,然后你可以通过询问它的顶视图控制器来获得所需的视图。