Search code examples
iosnsnotificationcenterdelegation

Adding observer after poster NSNotificationCenter workaround?


Is it possible to assign a delegate or NSNotificationCenter observer to VC2 that is loaded after VC1, where post takes place?

I have a tabbar app with multiple VCs. VC 1 is loaded first and the action to trigger a post takes place before VC2 is loaded. In VC2, I need to copy or get reference of an array from VC1.

Is there any other way to do this? Please help! I've been at this for 4 hours now. Thanks


Solution

  • try this it may help you.

    FirstViewController

    -(void)viewDidAppear:(BOOL)animated
    {
        NSArray *temp=[NSArray arrayWithObjects:@"1",@"2", nil];
    
        [[NSNotificationCenter defaultCenter] postNotificationName:@"postArrayObject" object:temp];
    
    }
    

    SecondViewController

    -(void)viewWillAppear:(BOOL)animated
    {
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objFirstViewController:) name:nil object:nil];
    
    }
    -(void)objFirstViewController:(NSNotification *)notification
    {
        if ([[notification name]isEqualToString:@"postArrayObject"])
        {
            NSArray *cellData = [notification object];
            if (cellData)
            {
                UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"WORKING"
                                                                  message:nil
                                                                 delegate:nil
                                                        cancelButtonTitle:@"OK"
                                                        otherButtonTitles:nil];
    
                [message show];
            }
    
        }
    
    }