【发布时间】:2016-02-08 01:21:25
【问题描述】:
我正在尝试实现发布在 StackOverflow 上的这个答案
use block to pass data from modal view to parent view
当我尝试在 SecondViewController(模态视图)的实现中设置 vale 以阻止。我收到错误的内存访问错误。
我仍在学习 Objective C 并且使用协议我能够做到这一点,但块似乎更有效。 谁能告诉我我在哪里犯错了。这是我的 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 //模态视图
#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
【问题讨论】:
标签: ios objective-c xcode objective-c-blocks