我有一个 prepareforsegue 方法,可以拍照并在不同的视图控制器上显示。所以我需要在显示图片的同时在 secondviewcontroller 上设置一个标签,稍后我将在该标签上做一个随机单词生成器,但现在我只需要设置标签。我尝试将它放在 prepareforsegue 方法中,但它给了我一个错误。这是我的所有代码:
//ViewController.m
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"CameraSegue"] || [segue.identifier isEqualToString:@"LibrarySegue"])
{
UIImagePickerController *controller = [segue destinationViewController];
controller.sourceType = [segue.identifier isEqualToString:@"LibrarySegue"] ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera;
controller.delegate = self;
}
else if([segue.identifier isEqualToString:@"ShowImageViewController"]){
UIImage *image = (UIImage*)sender;
ShowImageViewController *viewController = segue.destinationViewController;
viewController.pickedImage = image;
UILabel *label = (UILabel *) sender;
ShowImageViewController *vc = segue.destinationViewController;
vc.cap = label;
//I tried to set the label here
label.text = @"Hello";
}
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:^{
picker.delegate = nil;
[self performSegueWithIdentifier:@"ShowImageViewController" sender:image];
}];
}
//SeconViewController.h
@property(nonatomic, strong) UIImage *pickedImage;
@property (weak, nonatomic) IBOutlet UIImageView *pickedImageView;
@property(nonatomic, retain) IBOutlet UILabel *cap;
//SecondViewController.m
-(void)viewWillAppear:(BOOL)animated{
self.pickedImageView.image = self.pickedImage;
}