如何从一个视图访问数据到另一个视图? [英] How to access data from one view to another view?

查看:139
本文介绍了如何从一个视图访问数据到另一个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITabBarController ,带有两个标签:

I have an UITabBarController with two tabs:


  1. UINavigationController

  2. OptionsViewController:UIViewController

  1. UINavigationController
  2. OptionsViewController : UIViewController

如何在 OptionsViewController UILabel.text ) >,在一个新增的模态视图中,该视图已从 UINavigationController 调用?

How can I reach data (ie. UILabel.text) set in OptionsViewController, in a new added modal View which has been invoked from UINavigationController?

编辑1:

场景:启动应用程序后,我选择第二个名为选项的标签栏,我填写一个textField。标签设置为 textField 的值。接下来,我选择第一个名为Main的标签栏,其中有一个按钮。我点击按钮,出现新的模态视图。在这个新的模态视图中,我想显示 textField

Scenario: After launching app I select the second tab bar called "Options" where I fill up a textField. The label is set to value from textField. Next I select first tab bar called "Main" where I have a button. I click the button and new modal View appears. In this new modal View I'd like to show the value from textField

推荐答案

我喜欢MVC ,但我不是一个绝对的纯粹主义者,为了完成一个相当微不足道的任务而伤害自己,所以你在这里得到的答案是好的和有用的。但是,通过创建一个ivar来引用特定类型(如标签或其他视图控制器),您可以将不需要耦合的事物耦合在一起。你可以做的是让你的第一个标签视图控制器成为你的第二个标签视图控制器的委托。所以在你的app delegate中做这样的事情。

I love MVC, but I'm not an absolute purist to the point of hurting yourself to accomplish a fairly trivial task, so the answers you've gotten here are good and useful. However, by creating an ivar to refer back to a specific type such as a label or other view controller, you are coupling things together that aren't necessary to couple. What you could do instead is make your first tab view controller a delegate of your second tab view controller. So do something like this in your app delegate.

OptionsViewController *optionsViewController = // ... get this from the tab view
FirsTabViewController *firstTabViewController = // ... same here

[optionsViewController setDelegate:firsTabViewController];

这意味着您需要在OptionsViewController中使用ivar:

Which means that you need an ivar in your OptionsViewController:

@property (assign) id delegate;

然后,当您要触发的任何事件发生在您的选项视图控制器中时,请查看是否委托可以响应您命名的选择器。例如:

Then, when whatever event you want to trigger the change occurs in your options view controller, see if the delegate can respond to a selector you've named. For example:

- (void)someEventHappenedLikeTyping:(id)sender;
{
    if ([delegate respondsToSelector:@selector(setOptionsString:)]
        [delegate performSelector:@selector(setOptionsString:) withObject:[label text]];
}

请注意,您从未指定任何特定的对象类型。您只需检查代理是否被声明为id)可以响应那个选择器。如果可以的话,它会按照它所说的去做,否则就是沉默。

Notice you never specified any specific object types. You just check to see if the delegate (which was declared as id) can respond to that selector. If it can, it does what it's told and just is silent otherwise.

为了实现这一点,你需要一个用于optionsString的ivar在你的FirstTabViewController中,它将在标题中声明为:

For this to work, you need an ivar for the optionsString in your FirstTabViewController and so it would be declared in the header as:

@property (copy) NSString *optionsString;

然后在.m中对它进行@synthesize。这会导致-setOptionsString成为一个有效的选择器,它将被调用-someEventHappenedLikeTyping方法。

and then @synthesize it in the .m. This causes -setOptionsString to become a valid selector that will get called in the -someEventHappenedLikeTyping method.

无论如何,现在,如果您需要更改哪个视图控制器引用哪个,您不必进入标题并更改(典型值)引用的ivar e。您只需要在视图控制器中实现选择器(顺便说一下,这称为非正式协议),视图控制器是选项视图控制器的委托。

Anyhow, now, if you ever need to to change which view controller references which, you don't have to go into the header and change the type of ivar referenced. You simply need to implement the selector (this is known as an informal protocol, by the way) in the view controller that is a delegate of your options view controller.

Just那里有一些思考的东西。希望有所帮助。可以在我添加的代码中进行进一步的解耦,但同样对于这样一个简单的任务来说可能有点过分。如果您需要澄清或想通过进一步脱钩来理解我的意思,请告诉我。

Just some food for thought there. Hope that helps. There is further de-coupling that could be done in the code I've added, but again it may be overkill for such a simple task. Let me know if you need clarification or want to understand what I mean by further decoupling.

祝你好运,

PS 有时需要在两个标签栏视图控制器之间共享数据,这意味着您有设计缺陷。如果您想要从选项视图中存储首选项,则只需调用

p.s. Sometimes needing to share data between two tab bar view controllers, means you have a design flaw. If you are wanting to store preferences from your options view, you should just call

[[NSUserDefaults standardUserDefaults] setObject:[label text] forKey:@"option1"];
[[NSUserDefaults standardUserDefaults] synchronize];

然后你可以从主要标签中取回NSUserDefaults;

Then you can pull from the NSUserDefaults back in your main tab with;

NSString *option1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"option1"];
// Do something with option1

这篇关于如何从一个视图访问数据到另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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