Pass value to parent controller when firing controller

I want to send data to parentviewcontroller, but the following code will work. Give me a solution

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

Here Post is the name of the controller from which I call the presentViewController:animated:completion method.

+4
source share
3 answers

vc does not seem to initialize.

Perhaps you can do it,

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

Since the view manager you want to influence is located by the parentViewController , you should be able to create and set the abc property.

0
source

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]; } 

Follow 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 the model is canceled, your NotificationHandler will be executed, and everything that you pass as an object will receive a selection in the parent class. Please ask, some clarification is still needed.

+20
source

Take it in the .h file in the ParentViewController

 NSString *strABC; 

Make function below in ParentViewController

 -(void)setString:(NSString *)strEntered{ strABC=strEntered; } 

Now in the Post Post view controller do the following:

 ParentViewController *objSecond = [[ParentViewController] initwithNibName:@"parentView.xib" bundle:nil]; [objSecond setString:@"Comment Controller"]; [self.navigationController pushViewController:objSecond animated:YES]; [objSecond release]; 

Now, in the secondViewController viewWillAppear method, write this.

 -(void)viewWillAppear:(BOOL)animated{ lblUserInput.text = strABC; } 

Please check spelling errors as I wrote this. Hope this help.

If you are not using a UINavigationContoller , you can do something like this.

 SecondViewControler *objSecond = [[SecondViewController] initwithNibName:@"secondview.xib" bundle:nil]; [objSecond setUserInput:txtUserInput.text]; [objSecond viewWillAppear:YES]; [self.view addSubview:objSecond]; [objSecond release]; 
+1
source

All Articles