如何从一个 ViewController 访问对象到另一个 ViewController [英] How to Access Objects from one ViewController to another ViewController

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

问题描述

提供一些提示以摆脱以下情况.

Provide some tips to get rid of the following scenario.

说明:

我有两个 viewController,分别是 ViewController1ViewController2,所以很明显我们有 ViewController1.h, ViewController1.mViewController2.hViewController2.m.现在我声明了一个

I have two viewControllers namely ViewController1 and ViewController2, so obiviously we have ViewController1.h, ViewController1.m and ViewController2.h, ViewController2.m. Now I declared an

NSString *string1;

ViewController1.h 中并将其声明为属性

in ViewController1.h and declared it as a property

@property(nonatomic,retain) NSString *string1;

并在ViewController1.m中合成为

@synthesize string1;

ViewController1.m 中,我将 string1 值设置为

and in ViewController1.m, i set the string1 value as

string1=@"Hello Every One";

同样我声明

NSString *string2;

ViewController2.h 中并将其声明为属性

in ViewController2.h and declared it as a property

@property(nonatomic,retain)NSString *string2;

并在ViewController2.m中合成为

@synthesize string2;

如果我想将 string1 值(在 ViewController1.m 中)设置为 string2(在 ViewController2.m 中>),我该怎么做?

If I want set the string1 value (in ViewController1.m) to string2 (in ViewController2.m), how can I do that?

推荐答案

这取决于您要在其中设置 string1 的代码正在运行的位置.如果它在可以访问两个视图控制器对象的某个外部类中,那么它很简单.如果你有 ViewController1 对象 vc1 和 ViewController2 对象 vc2,那么你要做的就是:

That depends on where the code you want to set string1 in is being run. If it is in some outside class with access to both view controller objects, then it's simple. If you have ViewController1 object vc1, and ViewController2 object vc2, then all you do is:

[vc1 setString1:[vc2 string2]];

如果您想从 ViewController2 中运行的代码中设置 string1,那么您可以使用通知机制.在 ViewController1 的初始化例程中,您输入:

If you want to set string1 from code run within ViewController2, then you use the notification mechanism. In ViewController1's initialization routine, you put:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aChangeStringMethod:) name:@"anyStringJustMakeItUnique" object:nil];

并定义:

-(void)aChangeStringMethod:(NSNotification)notification{
     string1 = [((ViewController2 *)[notification object]) string2];
}

然后,在ViewController2中,当你想改变字符串时:

Then, in ViewController2, when you want to change the string:

[[NSNotificationCenter defaultCenter] postNotificationName:@"anyStringJustMakeItUnique" withObject:self];

当您从可以访问 vc2 但不能访问 vc1 的某个第三类更改字符串时,使用相同的技术.ViewController1 代码同上,当你想改变字符串时:

The same technique is used when you are changing the string from some third class that has access to vc2 but not vc1. ViewController1 code is the same as above, and when you want to change the string:

[[NSNotificationCenter defaultCenter] postNotificationName:@"anyStringJustMakeItUnique" withObject:vc2];

最棘手的部分是如果您想从 ViewController1 中更改字符串(假设您无权访问对象 vc2).您必须使用两个通知:上面的一个,以及 ViewController2:

The trickiest part is if you want to change the string from within ViewController1 (assuming that you don't have access to the object vc2). You have to use two notifications: the one above, and also, for ViewController2:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(launchTheOtherNotificationMethod:) name:@"anotherNotificationName" object:nil];

-(void)launchTheOtherNotificationMethod:(NSNotification)notification{
     [[NSNotificationCenter defaultCenter] postNotificationName:@"anyStringJustMakeItUnique" withObject:self];
}

然后,当你想改变字符串时:

Then, when you want to change the string:

[[NSNotificationCenter defaultCenter] postNotificationName:@"anotherNotificationName" withObject:nil];

如果您认为这太复杂或造成太多开销,更简单的解决方案是让 ViewController1 和 ViewController2 中的字段相互指向彼此.然后,在 ViewController1 中:

If you think this is too complex or causes too much overhead, the simpler solution is to have, as fields in ViewController1 and ViewController2, pointers to one another. Then, within ViewController1:

string1 = [myVC2 string2];

如果你使这些字段成为属性,那么,从外部:

And if you make these fields properties, then, from the outside:

[vc1 setString1:[[vc1 myVC2] string2]];

甚至:

[[vc2 myVC1] setString1:[vc2 string2]];

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

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