问题描述
我在我的应用中使用 Storyboard
,我想将数据从一个视图传递到另一个视图。
I am using Storyboard
in my app and I want to pass data from one view to another view.
我使用 instantiateViewControllerWithIdentifier
而不是使用segues。在这种情况下,我从我的第一个 TableViewController
实例化到 NavigationController
,它有第二个 TableViewController
附加,因为我需要在第二个 TableViewController
中导航。现在我想将第一个 TableviewController
中的数据传递给我的第二个 TableviewController
,具体取决于哪一行被点击。在这种情况下, newTopViewController
将是我的 NavigationController
但我现在的问题是如何从传递数据firstTableViewController
到 secondTableviewController
。
Instead of using segues I am using instantiateViewControllerWithIdentifier
. In this case I am instantiate from my first TableViewController
to a NavigationController
which has a second TableViewController
attached because I need the navigation in the second TableViewController
. Now I want to pass data from my first TableviewController
, depending which row was clicked, to my second TableviewController
. In this case newTopViewController
would be my NavigationController
but my problem is now how to pass data from firstTableViewController
to the secondTableviewController
.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:@"%@Top", [menuArray objectAtIndex:indexPath.row]];
UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
}
推荐答案
如果你实例化一个navigationController,你可以使用 viewControllers
属性来获取导航控制器的内部viewController。
If you instantiate a navigationController, you can use the viewControllers
property to get the inner viewController of the navigation controller.
这样的事情:
UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
MBFancyViewController *viewController = navigationController.viewControllers[0];
// setup "inner" view controller
viewController.foo = bar;
[self presentViewController:navigationController animated:YES completion:nil];
这篇关于instantiateViewControllerWithIdentifier并传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!