To pass data back to the previous view controller, you will need to create a delegate protocol the second view controller uses and exposes to the previous view controller. The sample delegate shown in this example forces implementation of the dataFromController: method by the adopter. The second view controller exposes the delegate protocol through the delegate property.
@protocol SecondViewControllerDelegate <NSObject>
- (void)dataFromController:(NSString *)data;
@interface SecondViewController : UIViewController
@property (nonatomic, retain) NSString *data;
@property (nonatomic, weak) id<SecondViewControllerDelegate> delegate;
On the first view controller we need to adopt this delegate protocol.
@interface FirstViewController () <SecondViewControllerDelegate>
Now that the delegate protocol is adopted, we must implement the required methods. In this method, our sample sets the label text to the data passed in and disables the button.
- (void)dataFromController:(NSString *)data
_label.text = data;
_button.enabled = NO;
For this method and adoption of the delegate protocol to work, we must set the delegate property to self.
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.data = _label.text;
secondViewController.delegate = self; // Set the second view controller's delegate to self
[self.navigationController pushViewController:secondViewController animated:YES];