1

I am trying to pass the data on the back button of the navigation bar in my project, help me.

Imad Ali
  • 3,261
  • 1
  • 25
  • 33
Shubh
  • 11
  • 3

2 Answers2

0

I have two View Controllers.I am passing the data from Second View Controller to First View Controller using Custom Delegate.

Here I used the Custom Delegate and NSNotification Center method.

Custom Delegate

First we can use Custom Protocol Delegate

@class SecondViewController;

@protocol SecondViewControllerDelegate <NSObject>

- (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text;

@end

Then we need to Assign the delegate here.

@property (nonatomic, assign)id<SecondViewControllerDelegate> delegate;

Very importantly we have to set the Custom Delegate Method in the Second View Controller.Because where we send the data to first view controller once we click the done button in second view controller.

[self.delegate secondViewController:self didEnterText:self.nameTextField.text];

Finally we must call the Custom Delegate Method in First View Controller, where we get the data and assign that data to label.Now you can see the passed data using custom delegate.

#import "SecondViewController.h"

@interface ViewController : UIViewController<SecondViewControllerDelegate>

When you need to call Custom Delegate in First View Controller.m

- (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text { self.labelName.text = text; //Getting the data and assign the data to label here. }

But First when you navigate to Second View Controller, you need to do below things

-(IBAction)gotoNextView:(id)sender;

{

//Set the delegate here

secondViewController.delegate = self;

.....//navigating code

}

SecondViewController.h

#import <UIKit/UIKit.h>
@class SecondViewController;
@protocol SecondViewControllerDelegate <NSObject>
- (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text;
@end
@interface SecondViewController : UIViewController
@property (nonatomic, assign)id<SecondViewControllerDelegate> delegate;  
@property (nonatomic, strong) IBOutlet UITextField *nameTextField;//It must connect as outlet connection
- (IBAction)doneButtonTapped:(id)sender;
@end

SecondViewController.m

#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self) {
    // Custom initialization
   }
   return self;
}

- (void)viewDidLoad
{
     [super viewDidLoad];
     // Do any additional setup after loading the view.
}

//Either use NSNotification or Delegate
- (IBAction)doneButtonTapped:(id)sender;
{
          //Use Notification
     [[NSNotificationCenter defaultCenter] postNotificationName:@"passingDataFromSecondViewToFirstView" object:self.nameTextField.text];
          //OR Custom Delegate
     [self.delegate secondViewController:self didEnterText:self.nameTextField.text];
     [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
}

Then

ViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface ViewController : UIViewController<SecondViewControllerDelegate>
@property (nonatomic, strong) IBOutlet UILabel *labelName; //You must connect the label with outlet connection
- (IBAction)gotoNextView:(id)sender;
@end

ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
   [super viewDidLoad];
   //addObserver here...
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFromPreviousViewControllerNotificationReceived:) name:@"passingDataFromSecondViewToFirstView" object:nil];
   // Do any additional setup after loading the view, typically from a nib.
}

//addObserver Method here....
- (void)textFromPreviousViewControllerNotificationReceived:(NSNotification *)notification
{
   // set text to label...
   NSString *string = [notification object];
   self.labelName.text = string;
}
- (IBAction)gotoNextView:(id)sender;
{
   //If you use storyboard
   SecondViewController *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
   //OR  If you use XIB
   SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
   secondViewController.delegate = self;
   [self.navigationController pushViewController:secondViewController animated:YES];
}

//Calling custom delegate method
- (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text
{
   self.labelName.text = text; //Getting the data and assign the data to label here.
}

- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
 }
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

You can detect when a back button is pressed, in viewDidDisapear:

-(void)viewDidDisappear:(BOOL)animated{
     if (self.isMovingFromParentViewController) {
        //moving back
        //pass to viewCollection delegate and update UI
        [self.delegate passBackSavedData:self.dataModel];
     }
}
Iosif
  • 353
  • 2
  • 15