如何使用 StoryBoard 在 NavigationController 和 ViewController 之间传递值? [英] How can I pass value between NavigationController and ViewController with StoryBoard?

查看:17
本文介绍了如何使用 StoryBoard 在 NavigationController 和 ViewController 之间传递值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,
以下是我的故事板,
第一个控制器是一个 TabBarController,
它与 A (ViewController) 的关系.

I have a problem,
The following is my StoryBoard,
the first Controller is a TabBarController,
and it relation with A (ViewController).


A 是一个 ViewController,
B 是一个 NavigationController,A 通过 modal segue
将页面更改为 BC 是一个 ViewController,C 会通过 push 切换到另一个页面,所以我需要一个 NavigationController

好的,我想将值从 A 传递到 C,
现在我可以通过 prepareForSegue,
将值从 A 传递到 B但是,因为B和C有关系但没有segue,
所以我不能通过 prepareForSegue 将值从 B 传递到 C !!!

如何使用 StoryBoard 在 NavigationController 和 ViewController 之间传递值?


A is a ViewController,
B is a NavigationController, A change page to B by modal segue
C is a ViewController, C will change to another page by push so I need a NavigationController

OK, I want to pass value from A to C,
now I can pass value from A to B by prepareForSegue,
However, because B and C have relationship but not segue,
So I can't pass value from B to C by prepareForSegue!!!

How can I pass value between NavigationController and ViewController with StoryBoard?

推荐答案

这里的 Storyboard 图像有点误导.

The Storyboard image is a little misleading here.

当你转向 B 时,实际上你正在转向 B/C 组合,因为 NavControllers 的堆栈中总是至少有一个 viewController(这是他们的 topViewController 和他们的 [viewControllers objectAtIndex:0]).

When you segue to B, actually you are segueing to the B/C combo as NavControllers always have at least one viewController in their stack (which is their topViewController and their [viewControllers objectAtIndex:0]).

所以你确实有直接从 A 到 C 的关系.

So you do have a relationship directly from A to C.

您如何访问该控制器取决于您的 segue 是 modal 还是 push.在您的情况下,它是模态的,但我将描述两者,以便您可以看到差异.

How you access that controller depends on whether your segue is modal or push. In your case it is modal, but I will describe both so you can see the difference.

无论哪种情况,要将数据传递给 C,您都需要在其头文件中声明一个属性

In either case, to pass data to C, you need to declare a property in it's header file

    @interface CviewController: UIViewContrller
    @property (assign) int dataFromA;
    @end

推送转场

在推送segue中,实际上是C是destinationViewController,而不是B.实际上推送segue是由B管理的,它是A和C的UINavigationController.推送segue背后的代码是这样的形式
[self.navigationController pushViewController:otherViewController];

In a push segue, it is actually C that is the destinationViewController, not B. In fact the push segue is governed by B, which is the UINavigationController for both A and C. The code behind the push segue is of the form
[self.navigationController pushViewController:otherViewController];

在 AviewController 的 prepareForSegue 中:

In AviewController's prepareForSegue:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
        {
         CviewController* controller = segue.destinationViewController;
        [controller setDataFromA:self.data];
    }

在故事板中可能在两个不共享一个公共 UINavigationController 的视图控制器之间创建一个推式转场线.但是,当你运行它时,你会得到一个崩溃错误:

It is possible in the storyboard to make a push segue line between two viewControllers that do not share a common UINavigationController. However when you run this you will get a crash error:

'无法为 segue 'pushC' 找到导航控制器.Push segues 只能在源控制器由 UINavigationController 的实例管理时使用.'

'Could not find a navigation controller for segue 'pushC'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'

在每一个好的 push segue 背后都有一个导航控制器.

Behind every good push segue lies a Navigation Controller.

模态转场

隐藏在模态 Segue 后面的代码是 UIViewController 方法
- (void)presentViewController:(UIViewController *)viewControllerToPresent

The code hiding behind a modal Segue is the UIViewController method
- (void)presentViewController:(UIViewController *)viewControllerToPresent

在 NavController/ViewController 组合的模态转场中,目标 viewController 是转场线指向的任何内容.如果它指向一个viewController,那就是segue.destinationController(UINavigationController 将被忽略,这不是你想要的);如果它指向一个 UINavigationController,就像在这种情况下一样,那个 将是它的 destinationController.但是访问 viewController 仍然很简单,因为它将是导航控制器的 topViewController.

In a modal segue to a NavController/ViewController combo, the destination viewController is whatever the segue line points to. If it points to a viewController, that is the segue.destinationController (and the UINavigationController will be ignored, which is not what you want here); if it points to a UINavigationController, as in this case, that will be it's destinationController. But it is still straightforward to access the viewController, as it will be the navigation Controller's topViewController.

在 AviewController 的 prepareForSegue 中:

In AviewController's prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
     CviewController* controller = 
        (CviewController*)[[segue destinationViewController] topViewController];
    [controller setDataFromA:self.data];
}

请注意,在这种情况下,我们必须使用旧式 [[消息传递] 语法].如果我们使用 modern.property.syntax 我们会得到一个编译错误.那是因为程序不知道 desinationViewController 的类型,并且拒绝接受 topViewController 作为未知类型的属性.但它很高兴向未知类型[发送[真实消息]].我们还必须 (typecast*) 避免编译器警告.

Note that in this case we have to use old-style [[message passing] syntax]. If we use modern.property.syntax we get a compile error. That's because the program does not know the type of desinationViewController, and refuses to accept topViewController as a property of an unknown type. But it is happy to [send [real messages]] to an unknown type. We also have to (typecast*) to avoid compiler warnings.

这篇关于如何使用 StoryBoard 在 NavigationController 和 ViewController 之间传递值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆