在两个视图控制器之间传递对象

时间:2011-08-22 06:12:06

标签: iphone objective-c ipad object nsmanagedobject

我正在尝试在2个VC之间传递一个对象,从弹出框到分割视图控制器的详细视图。

我想我需要使用NSNotificationCenter。

我试过这个,但似乎无法让它发挥作用。

didSelectRow的popover中

  [[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" withObject:objectToPass];

detail VC

    - (void) didReceiveNotificationPassObject:(NSNotification*)notification  
    {
        YourObjectClass *theObject = (YourObjectClass*)notification.object;
    }

    - (void)viewDidLoad 
    {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotificationPassObject:) name:@"PassObject" object:nil];
    }

3 个答案:

答案 0 :(得分:0)

您面临的问题是什么? didReceiveNotificationPassObject:命中了吗?如果没有,您可以验证viewDidLoad之前执行didSelectRow

使用[[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" object:objectToPass];代替[[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" withObject:objectToPass];

另外,请不要忘记removeObserver中的viewDidUnload

HTH,

阿克沙伊

答案 1 :(得分:0)

输入问题时可能只是一个拼写错误,但在发布通知的第一行

[[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" withObject:objectToPass];

方法签名错误 - 它应该是'object:objectToPass'而不是'withObject:objectToPass'。你在那里的行将在运行时编译并发出警告并崩溃。

除此之外,所有逻辑似乎都很好。

答案 2 :(得分:0)

使用多个参数进行通知的快速简便的解决方案是调用此通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"shareButton" object:@"camera"];

“相机”就像你的参数一样。然后

- (void)shareButton:(id)sender
{
   NSString *kindOf = [sender object];

    if ([kindOf isEqualToString:@"camera"]) {
         // Your code goes here
    }
 }
相关问题