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;
@end

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,

-(void)viewWillDisappear:(BOOL)animated
{
[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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s