Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass value to parent controller when dismiss the controller

I want to send data to parentviewcontroller but the following code crashes. Give me the solution

Post *vc;
vc.abc =@"Comment Conttroller";
[self.parentViewController dismissModalViewControllerAnimated:YES];

Here, Post is the controller name from where I am calling the presentViewController:animated:completion method.

like image 444
Saad Ur Rehman Avatar asked Nov 29 '22 04:11

Saad Ur Rehman


1 Answers

Put this in your parent controller in viewDidLoad

// get register to fetch notification  
[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(yourNotificationHandler:)
                                      name:@"MODELVIEW DISMISS" object:nil];
// --> Now create method in parent class as;
// Now create yourNotificationHandler: like this in parent class
-(void)yourNotificationHandler:(NSNotification *)notice{
    NSString *str = [notice object];
}

Put following to your child class where

-(void)dissmissModelView{

    [self dismissModalViewControllerAnimated:YES];
    NSLog(@"DismissModalviewController");

    //raise notification about dismiss 
    [[NSNotificationCenter defaultCenter] 
          postNotificationName:@"MODELVIEW DISMISS" 
                        object:@"Whatever you want to send to parent class"];  
}

as soon as model view get dismiss yourNotificationHandler get executed and whatever you pass as an objet will get fetch in your parent class. Please ask if still need some clarification.

like image 200
mhrrt Avatar answered Dec 09 '22 16:12

mhrrt