从this answer开始,似乎有一种方法可以使用Swift闭包将数据传递回先前的View Controller,这比使用委托更简单。
鉴于Swift闭包类似于在Objective-C中使用块,任何人都可以帮助我在Objective-c项目中执行此操作吗?我不知道Swift和委托方法设置一个属性似乎过于复杂。
这是快速代码:
For sending a UIImage back in my case. In ViewControllerA:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let cropImageViewController = segue.destinationViewController as! CropImageViewController
cropImageViewController.choseImage = { image in
//Do what you want in here!
}
}
In ViewControllerB:
var choseImage : (UIImage -> Void)?
@IBAction func done(sender: AnyObject) {
choseImage?(croppedImage)
self.dismissViewControllerAnimated(true, completion: nil)
}
我可以在Objective-C中使用Prepare for Segue传递数据,但我很难知道如何向后传递数据。
//launch VC
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"edit"]) {
...
destViewController.contact=contact;
}
//set property in presentingVC--THIS DOES NOT WORK.
self.presentingViewController.afterSave = YES;
[self dismissViewControllerAnimated:YES completion:nil];
答案 0 :(得分:0)
以下是裁剪视图控制器中需要执行的操作:
界面(.h):
@interface CropImageViewController : UIViewController
/// Block that returns void , called 'choseImage' and passes back UIImage.
@property (nonatomic,strong) void(^choseImage)(UIImage *image);
@end
实施(.m):
@implementation CropImageViewController
-(void)done{
// Check if block not nil
if(self.choseImage){
// Block usage with UIImage
self.choseImage(croppedImage);
}
}
@end
并为segue做准备:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"edit"]) {
CropImageViewController *cropVC = [segue destinationViewController];
[cropVC setChoseImage:^(UIImage *image) {
// Use image
}];
}
}