How to pass data back to previous ViewController using PresentViewController

Let suppose, We have to pass data from B to A ViewController. Here, I am using Present View Controller. When you  dismiss PresentViewController, Data will back to Parent View Controller.

In the B viewController.h File,

@protocol senddata <NSObject>
-(void) Passdatatoback:(NSString*)Passdataback;

Also set the property in .h file,

@property(nonatomic, assign)id delegate;

Now, move to B viewController.m File,

@synthesize delegate;

and set a method,

[delegate Passdatatoback:@”This is data which you want in A ViewController”];

Now, Move to A viewController,

Create a same method which you have create in B ViewController.m file.

-(void) Passdatatoback:(NSString*)Passdataback{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Your string Data Showing” message:Passdataback delegate:self cancelButtonTitle:@”OK “ otherButtonTitles:nil];
 [alert show];

In A ViewContrller.m file, When You go in B ViewController,

BViewConoller *BVC = [[BViewConoller alloc]init];
BVC.delegate= self;
[self.navigationController presentViewController:BVC animated:YES completion:nil];

That’s It.

If you like this post, Please comment below or subscribe this blog.

Leave your comments