I am trying to implement this answer posted on StackOverflow
use block to pass data from modal view to parent view
When I try to set vale to block in my implementation of SecondViewController(Modal view). I receive an error of bad memory access.
I am still learning Objective C and using protocol I am able to do this but block seems more effective. Could anyone please tell me where am I making mistake. Here are the codes and images of my StoryBoard.
FirstViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController
@end
FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
SecondViewController *second = [[SecondViewController alloc]init];
second.somethingHappenedInModalVC = ^(NSString *response) {
NSLog(@"Something was selected in the modalVC, and this is what it was:%@", response);
};
}
@end
SecondViewController.h //modal view
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface SecondViewController : UIViewController
@property (nonatomic, copy) void (^somethingHappenedInModalVC)(NSString *response);
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@property (strong, nonatomic) IBOutlet UITextField *textField;
@end
@implementation SecondViewController
- (IBAction)closeView:(id)sender {
self.somethingHappenedInModalVC(@"Sending the message"); //here memory warning display.
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end