0

我有一个 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;
}
4

1 回答 1

0

您正在将其投射sender到两个非常不同的事物上。

UIImage *image = (UIImage*)sender;
ShowImageViewController *viewController = segue.destinationViewController;
viewController.pickedImage = image;

UILabel *label = (UILabel *) sender;
ShowImageViewController *vc = segue.destinationViewController;
vc.cap = label;
label.text = @"Hello";

您不能将 UIImage 转换为 UILabel 并期望事情顺利进行。在prepareForSegue中,您发送的是图像,而不是标签。

您可以将底部四行更改为:

viewController.cap.text = @"Hello";

ShowImageViewController在这种情况下,另一种选择是像为图像所做的那样定义一个新属性。

//SecondViewController.h
@property(nonatomic, strong) UIImage *pickedImage;
@property (weak, nonatomic) IBOutlet UIImageView *pickedImageView;
@property(nonatomic, retain) IBOutlet UILabel *cap;
@property (nonatomic, strong) NSString pickedLabel; //New property

-(void)viewWillAppear:(BOOL)animated{
    self.pickedImageView.image = self.pickedImage;
    self.cap.text = self.pickedLabel;
}

然后在prepareForSegue

UIImage *image = (UIImage*)sender;
ShowImageViewController *viewController = segue.destinationViewController;
viewController.pickedImage = image;
viewController.pickedLabel = @"Hello";
于 2012-12-21T17:25:47.637 回答