2013-10-31 35 views
0

我很难理解父母和孩子如何沟通(以及他们如何将数据传递给对方)。我有两个简单的对象(两个ViewControllers)。我明白,父子关系应该允许我使用属性将两个变量从子对象传递到父对象。因为我包括Obj。 B到Obj A我假设A是父母,B是孩子。我也明白,孩子知道父母,但反之亦然,这是正确的吗?使用Obj.C中的属性将数据传递给对象

我收录了Obj。 B进入Obj。 A和我希望能够访问我在Obj的头文件中声明的一些变量。 B

有人能给我一个非常简单的例子,并帮助我结束我的困惑吗?非常感谢。

+0

要将数据从A传递给B,您可以轻松使用'property'。要访问从B到A的数据,你可以看一下'protocol'。 告诉我们,如果你使用故事板或不使用属性 –

+2

通过数据你是什么意思的“亲子关系”? – dasblinkenlight

+0

@JordanMontel,现在我只是试图将两个变量从A传递给B.我明白从B到A我需要使用协议。但我很难理解谁需要这些属性。 B能够访问A的属性,反之亦然? – Nactus

回答

1

要从ViewControllerA直传数据(对象或值)为ViewControllerB推或呈现ViewControllers,你需要做的是这样的:

(例如,通过一个的NSString一个ViewControllerBViewControllerA

向前传递数据,而无需故事板:

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的例子,但你可以传递任何对象克拉。

+0

谢谢乔丹,upvoted。所有的答案都是信息,但我正在寻找这样的事情,所以我会接受这个答案。谢谢。 – Nactus

+0

不客气:) –

0

您可以使用弱分配集合了循环引用的对象之一:

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 
0

创建自定义委托并将消息从一个类发送到另一个类。因此,行为将是一个类将是发件人和其他将收到。仅供参考遵循此: -

iOS Protocol/Delegate confusion?

0

我不认为这是一个良好的编程风格,但你可以使用单对许多不同类别的

就像那个之间共享数据: 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 
+0

谢谢你的单身设计模式。我有点尝试使用setter和getters在这种情况下D – Nactus

+0

(如果你有属性NSMutableString),你可以使用 '[Settings my] .mySharedString = @“some text”' 来调用setter和 'NSString * myText = [设置我] .mySharedString' 致电getter – Thorax

1

我想你已经明白了倒退。父母应该知道这个孩子。孩子不需要知道其父母。

家长可以有一个强烈的参考其孩子。 (ex)

//inside the parent class 
@property (nonatomic, strong) id childObject; 

小孩通常不会明确知道它的“父母”是什么,但它对代表的参照很弱。该委托可以是特定类型的类,也可以是符合特定协议的类型为id的泛型类。(EX)

//inside the child class 
@property (nonatomic, weak) id<SomeProtocol> delegate; 
+0

感谢Nick,upvoted。因此,代表将用于将数据从孩子传回给父母,对吗? – Nactus

+0

这是正确的,是的。 – Nick

0

您负责在视图控制器之间传递数据。您可以使用-parentViewController-childViewControllers,还可以使用weak引用进行循环引用。

如果您使用的故事板,也许这将是看看-performSegueWithIdentifier:sender:一个好主意。发送者可以用来在视图控制器之间传递数据。

此外,如果你使用的是故事板,有时- instantiateViewControllerWithIdentifier:是很方便的事情。

有多种方法可以做到这一点。

相关问题