Objective c iOS:一个视图可以看到在另一个视图中创建的对象吗

Objective c iOS:一个视图可以看到在另一个视图中创建的对象吗,objective-c,ios,Objective C,Ios,一整天 我一直在阅读有关在控制器之间传递数据的协议和委托 假设ViewControllerA在对象上创建实例: myObject = [[anObject alloc]init]; [myObject setProperty: value]; ViewControllerB如何访问myObject的属性?ViewControllerB如何意识到ViewControllerA创建的对象 谢谢,如果B位于A之后(即,它们是层次结构的),您可以将对象传递给B(在创建对象后或在prepareforsg

一整天

我一直在阅读有关在控制器之间传递数据的协议和委托

假设ViewControllerA在对象上创建实例:

myObject = [[anObject alloc]init];
[myObject setProperty: value];
ViewControllerB如何访问myObject的属性?ViewControllerB如何意识到ViewControllerA创建的对象

谢谢,

如果B位于A之后(即,它们是层次结构的),您可以将对象传递给B(在创建对象后或在
prepareforsgue
中):

bController.objectProperty = myObject;
如果两者同时对用户处于活动状态(比如通过选项卡栏),则可以使用通知。这与委托不同,因为关系更松散-发送对象不必知道有关接收对象的任何信息

// in A
[[NSNotificationCenter defaultCenter] 
   postNotificationName:ObjectChangedNOtificationName 
   object:self
   userInfo:dictionaryWithObject];
// in B
[[NSNotificationCenter defaultCenter] addObserver:self 
   selector:@selector(objectChanged:) 
   name:ObjectChangedNOtificationName 
   object:nil];
如果B位于A之后(即,它们是层次结构的),您可以将对象传递给B(在创建对象后或在
prepareforsgue
中):

bController.objectProperty = myObject;
如果两者同时对用户处于活动状态(比如通过选项卡栏),则可以使用通知。这与委托不同,因为关系更松散-发送对象不必知道有关接收对象的任何信息

// in A
[[NSNotificationCenter defaultCenter] 
   postNotificationName:ObjectChangedNOtificationName 
   object:self
   userInfo:dictionaryWithObject];
// in B
[[NSNotificationCenter defaultCenter] addObserver:self 
   selector:@selector(objectChanged:) 
   name:ObjectChangedNOtificationName 
   object:nil];

您可以使用
NSNotificationCenter
让有兴趣的人了解新对象。
通常这是在模型层中完成的,例如,我有一个
Person
对象。
person
.h文件中定义“新建人员通知”

extern NSString *const NewPersonCreatedNotification;  
在.m文件中

NSString *const NewPersonCreatedNotification = @"NewPersonCreatedNotification";  
创建人员时(在init方法中)发布通知

    [[NSNotificationCenter defaultCenter] postNotificationName:NewPersonCreatedNotification
                                                        object:self
                                                      userInfo:nil];  
然后,无论谁想要知道新创建的人员,都需要观察此通知,例如ViewControllerA想要知道,所以在它的init方法中,我会:

- (id)init
{
    self = [super init];
    if (self) {
       [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleNewPersonCreatedNotification:)
                                                     name:NewPersonCreatedNotification
                                                   object:nil];    
    }
    return self;
}  


- (void)handleNewPersonCreatedNotification:(NSNotification *)not
{
    // get the new Person object  
    Person *newPerson = [not object];  

    // do something with it...
}

您可以使用
NSNotificationCenter
让有兴趣的人了解新对象。
通常这是在模型层中完成的,例如,我有一个
Person
对象。
person
.h文件中定义“新建人员通知”

extern NSString *const NewPersonCreatedNotification;  
在.m文件中

NSString *const NewPersonCreatedNotification = @"NewPersonCreatedNotification";  
创建人员时(在init方法中)发布通知

    [[NSNotificationCenter defaultCenter] postNotificationName:NewPersonCreatedNotification
                                                        object:self
                                                      userInfo:nil];  
然后,无论谁想要知道新创建的人员,都需要观察此通知,例如ViewControllerA想要知道,所以在它的init方法中,我会:

- (id)init
{
    self = [super init];
    if (self) {
       [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleNewPersonCreatedNotification:)
                                                     name:NewPersonCreatedNotification
                                                   object:nil];    
    }
    return self;
}  


- (void)handleNewPersonCreatedNotification:(NSNotification *)not
{
    // get the new Person object  
    Person *newPerson = [not object];  

    // do something with it...
}

ViewControllerB可以是ViewControllerA的代理。这样,ViewControllerA可以在创建对象时将消息传递给ViewControllerB。ViewControllerB可以是ViewControllerA的代理。这样,ViewControllerA可以在创建对象时将消息传递给ViewControllerB。谢谢Mundi。我将查看通知。谢谢Mun我来看看通知。