我很难理解父母和孩子如何沟通(以及他们如何将数据传递给对方)。我有两个简单的对象(两个ViewControllers)。我明白,父子关系应该允许我使用属性将两个变量从子对象传递到父对象。因为我包括Obj。 B到Obj A我假设A是父母,B是孩子。我也明白,孩子知道父母,但反之亦然,这是正确的吗?使用Obj.C中的属性将数据传递给对象
我收录了Obj。 B进入Obj。 A和我希望能够访问我在Obj的头文件中声明的一些变量。 B
有人能给我一个非常简单的例子,并帮助我结束我的困惑吗?非常感谢。
我很难理解父母和孩子如何沟通(以及他们如何将数据传递给对方)。我有两个简单的对象(两个ViewControllers)。我明白,父子关系应该允许我使用属性将两个变量从子对象传递到父对象。因为我包括Obj。 B到Obj A我假设A是父母,B是孩子。我也明白,孩子知道父母,但反之亦然,这是正确的吗?使用Obj.C中的属性将数据传递给对象
我收录了Obj。 B进入Obj。 A和我希望能够访问我在Obj的头文件中声明的一些变量。 B
有人能给我一个非常简单的例子,并帮助我结束我的困惑吗?非常感谢。
要从ViewControllerA
直传数据(对象或值)为ViewControllerB
推或呈现ViewControllers,你需要做的是这样的:
(例如,通过一个的NSString一个ViewControllerB
从ViewControllerA
)
向前传递数据,而无需故事板:
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.aString = myString; // myString is the data you want to pass
[self presentViewController:viewControllerB animated:YES completion:nil];
使用UINavigationController
:
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.aString = myString;
[self.navigationController pushViewController:viewControllerB animated:YES];
和内部viewControllerB
,你需要对你的.h像@property
:
@property (nonatomic, strong) NSString *aString;
和您的m内,您检索此@property
:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"%@", _aString);
}
这是一个NSString的例子,但你可以传递任何对象克拉。
谢谢乔丹,upvoted。所有的答案都是信息,但我正在寻找这样的事情,所以我会接受这个答案。谢谢。 – Nactus
不客气:) –
您可以使用弱分配集合了循环引用的对象之一:
ObjectA.h
@class ObjectB
@interface ObjectA
@property (strong) ObjectB *parent;
@end
ObjectA.m
#import "ObjectA.h"
#import "ObjectB.h"
@implementation ObjectA
// methods
@end
ObjectB.h
@class ObjectA
@interface ObjectB
@property (weak) ObjectA *child;
@end
ObjectB.m
#import "ObjectB.h"
#import "ObjectA.h"
@implementation ObjectB
// methods
@end
创建自定义委托并将消息从一个类发送到另一个类。因此,行为将是一个类将是发件人和其他将收到。仅供参考遵循此: -
我不认为这是一个良好的编程风格,但你可以使用单对许多不同类别的
就像那个之间共享数据: Singleton.h
@interface Settings : NSObject
@property (nonatomic) NSString *mySharedString;
+ (Settings *)my;
- (id)init;
@end
Singleton.m
#import "Settings.h"
@implementation Settings
@synthesize mySharedString
static Settings *my = nil;
+ (Settings *)my
{
if (!my)
my = [Settings new];
return my;
}
- (id)init
{
self = [super init];
if (self){
//do some code
}
return self
}
@end
然后在任何类,你可以这样说
NSString *classString = [Settings my].mySharedString
我想你已经明白了倒退。父母应该知道这个孩子。孩子不需要知道其父母。
家长可以有一个强烈的参考其孩子。 (ex)
//inside the parent class
@property (nonatomic, strong) id childObject;
小孩通常不会明确知道它的“父母”是什么,但它对代表的参照很弱。该委托可以是特定类型的类,也可以是符合特定协议的类型为id
的泛型类。(EX)
//inside the child class
@property (nonatomic, weak) id<SomeProtocol> delegate;
您负责在视图控制器之间传递数据。您可以使用-parentViewController
或-childViewControllers
,还可以使用weak
引用进行循环引用。
如果您使用的故事板,也许这将是看看-performSegueWithIdentifier:sender:
一个好主意。发送者可以用来在视图控制器之间传递数据。
此外,如果你使用的是故事板,有时- instantiateViewControllerWithIdentifier:
是很方便的事情。
有多种方法可以做到这一点。
要将数据从A传递给B,您可以轻松使用'property'。要访问从B到A的数据,你可以看一下'protocol'。 告诉我们,如果你使用故事板或不使用属性 –
通过数据你是什么意思的“亲子关系”? – dasblinkenlight
@JordanMontel,现在我只是试图将两个变量从A传递给B.我明白从B到A我需要使用协议。但我很难理解谁需要这些属性。 B能够访问A的属性,反之亦然? – Nactus