Prepareforsegue设置标签

时间:2012-12-21 17:17:43

标签: objective-c label uistoryboardsegue

我有一个prepareforsegue方法拍摄照片并在不同的viewcontroller上显示它。所以我需要在图片显示的同时在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;
}

1 个答案:

答案 0 :(得分: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";