【发布时间】:2014-01-17 04:31:57
【问题描述】:
我对 iOS 的东西还很陌生,我认为我理解将数据传递给视图控制器。显然不是。我正在尝试将图像数据从父 ViewController 传递给以模态方式呈现的子 ViewController。我认为问题与将对象传递给模态呈现的视图控制器而不是推送到导航堆栈的方式有关。贝娄是父母和孩子与我遇到的问题相关的代码。
来自 Parent.m:
- (IBAction)sort:(id)sender {
SortSelectViewController *ssvc = [self.storyboard instantiateViewControllerWithIdentifier:@"sort"];
ssvc.backgroundImage = [self screenshot];
[self performSegueWithIdentifier:@"sort" sender:self];
//The segue is linked from my parent view controller to my child with the identifier "sort"
}
- (UIImage *) screenshot {
CGRect rect = CGRectMake(0, 20, 640, 1116);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:rect afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
return image;
}
这是在我的 Child.h 中
@property (nonatomic, strong) UIImage *backgroundImage;
这是我的 Child.m:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"%@", self.backgroundImage);
//This log will return nil when the program is run - clear sign data is not being correctly passed
self.backgroundImage = [self.backgroundImage applyDarkEffect];
self.imageView.image = self.backgroundImage;
}
我已经将我的子视图控制器导入到我的父类中
【问题讨论】:
-
尝试在appdelegate中携带图片,并在viewwillapper方法中分配子视图控制器中的图片