如何在两个类之间传递变量

时间:2014-04-15 18:09:03

标签: ios objective-c

所以我有第一堂课:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
    self.imageView.image = chosenImage;
    _camera_button.hidden = true;
    _camera_imageview.hidden = false;
    [_next_camera setEnabled:YES];
    NSData *imageData = UIImagePNGRepresentation(chosenImage);
    [self upload_selfie:imageData];

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

我想将变量imageData传递给这个类:

- (IBAction)upload_selfie:(NSData *)sender{
    PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imageData];

    // Save PFFile
    [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            // Hide old HUD, show completed HUD (see example for code)

            // Create a PFObject around a PFFile and associate it with the current user
            PFObject *userPhoto = [PFObject objectWithClassName:@"UserPhoto"];
            [userPhoto setObject:imageFile forKey:@"imageFile"];

            // Set the access control list to current user for security purposes
            userPhoto.ACL = [PFACL ACLWithUser:[PFUser currentUser]];

            PFUser *user = [PFUser currentUser];
            [userPhoto setObject:user forKey:@"user"];

            [userPhoto saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error) {
//                    _loader6.hidden = true;
//                    [self performSegueWithIdentifier:@"toquestion" sender:self];
                }
                else{
                    // Log details of the failure
                    NSLog(@"Error: %@ %@", error, [error userInfo]);
                }
            }];
        }
    }];

}

我尝试过的地方:

PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imageData];

但是我收到错误"使用未声明的标识符"?ces我应该在其他地方引用imageData

这些也是唯一的两个版本

请你解释为什么以及我能做些什么呢?

2 个答案:

答案 0 :(得分:2)

您收到此错误,因为您的变量名称为发件人

- (IBAction)upload_selfie:(NSData *)sender

您需要将代码更改为:

PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:sender];

PS。您似乎使用 upload_selfie:方法实现两个目标:a)接口构建器操作处理程序; b)保存照片的方法。最好采用两种不同的方法。

答案 1 :(得分:1)

您将参数传递为sender,因此您应该使用该名称:PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:sender];

但是,为了更容易理解,选择一个不同的名字会很好。