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

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

问题描述

我有问题,
$
以下是我的StoryBoard,

第一个Controller是 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 ,通过模态更改页面 segue $
C是一个 ViewController ,C将通过 push 更改为另一个页面所以我需要一个NavigationController



好​​的,我想将值从A传递给C,现在我可以通过<$ c将值从A传递给B $ c> prepareForSegue ,

但是,因为B和C有关系而不是segue,所以我不能将值从B传递到C by prepareForSegue !!!



如何在NavigationController和ViewController机智之间传递值h StoryBoard?


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?

推荐答案

故事板图片在这里有点误导。

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是模态还是推送。在你的情况下,它是模态的,但我会描述两者,所以你可以看到差异。

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

在push segue中,它实际上是C,它是destinationViewController,而不是B.实际上push segue由B管理,B是A和C的UINavigationController。 push 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的两个viewControllers之间推送segue线。但是当你运行它时会出现崩溃错误:

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.'

每个推送segue的背后都有一个导航控制器。

Behind every good push segue lies a Navigation Controller.

模态segue

隐藏在模态Segue背后的代码是UIViewController方法

- (void)presentViewController:(UIViewController *)viewControllerToPresent

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

modal segue到NavController / ViewController组合,目标viewController是segue线指向的任何东西。如果它指向一个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作为未知类型的属性。但它 很高兴[发送[真实消息]]到未知类型。我们还必须(类型转换*)来避免编译器警告。

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天全站免登陆