将PFObject分配给UILabel - 将PFUser转换为NSString值?

时间:2017-08-25 02:14:36

标签: ios objective-c uilabel pfobject

我制作了一个应用程序,以便用户上传附有说明的图像。然后两者都显示在滚动视图中。我的图像下方还有一个UILabel,它应该显示上传用户的详细信息以及上传日期。

但是,目前显示用户信息和日期的UILabel并没有以我想要的格式(例如"上传者:用户名,日期和#34;)而是我获得以下..

UILabel

以下是用户上传图片时使用的PFObject代码;

PFObject *imageObject = [PFObject objectWithClassName:@"ReportImageObject"];
        [imageObject setObject:file forKey:@"image"];
        [imageObject setObject:[PFUser currentUser] forKey:@"user"];
        [imageObject setObject:self.descriptionTextField.text forKey:@"comment"];

以下是我为UFabel分配PFObject值的地方;

NSDate *creationDate = reportObject.createdAt;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"HH:mm dd/MM yyyy"];

UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 210, reportImageView.frame.size.width, 15)];
infoLabel.text = [NSString stringWithFormat:@"Uploaded by: %@, %@", [reportObject objectForKey:KEY_USER], [df stringFromDate:creationDate]];
infoLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:9];
infoLabel.textColor = [UIColor blueColor];
infoLabel.backgroundColor = [UIColor clearColor];
[reportImageView addSubview:infoLabel];

术语KEY_USER定义为@"user"

让我感到困惑的是我的另一个标签(图片描述 - 可以在左上角的上传图片上隐约看到"测试3")访问descriptionFieldText.text,显示的确切是什么用户进入?即使我检查NSLogPFObject objectForKey相关联的UILabel,它显示的格式与不需要的-(void)getReportImages{ //Fetch images PFQuery *query = [PFQuery queryWithClassName:@"ReportImageObject"]; [query orderByDescending:KEY_CREATION_DATE]; [query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) { if (!error) { self.reportsObjectArray = nil; self.reportsObjectArray = [[NSArray alloc] initWithArray:objects]; [self loadReportViews]; } else { [self showErrorView:error]; } }]; } -(void)loadReportViews{ for (id viewToRemove in [self.reportsScroll subviews]) { if ([viewToRemove isMemberOfClass:[UIView class]]) [viewToRemove removeFromSuperview]; } int originY = 10; for (PFObject *reportObject in self.reportsObjectArray){ UIView *reportImageView = [[UIView alloc] initWithFrame:CGRectMake(10, originY, self.view.frame.size.width -20, 300)]; //Adds image PFFile *image = (PFFile*)[reportObject objectForKey:KEY_IMAGE]; UIImageView *userImage = [[UIImageView alloc] initWithImage:[UIImage imageWithData:image.getData]]; userImage.frame = CGRectMake(0, 0, reportImageView.frame.size.width, 200); [reportImageView addSubview:userImage]; //Adds image info NSDate *creationDate = reportObject.createdAt; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"HH:mm dd/MM yyyy"]; UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 210, reportImageView.frame.size.width, 15)]; infoLabel.text = [NSString stringWithFormat:@"Uploaded by: %@, %@", [reportObject objectForKey:KEY_USER], [df stringFromDate:creationDate]]; infoLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:9]; infoLabel.textColor = [UIColor blueColor]; infoLabel.backgroundColor = [UIColor clearColor]; [reportImageView addSubview:infoLabel]; //Adds image description UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 24, reportImageView.frame.size.width, 15)]; descriptionLabel.text = [reportObject objectForKey:KEY_COMMENT]; descriptionLabel.font = [UIFont fontWithName:@"ArialMT" size:13]; descriptionLabel.textColor = [UIColor whiteColor]; descriptionLabel.backgroundColor = [UIColor clearColor]; [reportImageView addSubview:descriptionLabel]; [self.reportsScroll addSubview:reportImageView]; originY = originY + reportImageView.frame.size.width + 20; NSLog(@"Test of description%@",descriptionLabel); NSLog(@"The content of infolabel %@", infoLabel); } self.reportsScroll.contentSize = CGSizeMake(self.reportsScroll.frame.size.width, originY); } 类似,但实际的Label会访问所需的值吗?

已更新 正在使用的主要方法

正在显示上传图像的视图控制器 - 显示UILabel的位置

- (void)sendPressed:(id)sender{
[self.descriptionTextField resignFirstResponder];

self.navigationItem.rightBarButtonItem.enabled = NO;

UIActivityIndicatorView *loadingSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

[loadingSpinner setCenter:CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0)];
[loadingSpinner startAnimating];

[self.view addSubview:loadingSpinner];

//Upload a new picture if not currently uploaded.
NSData *pictureData = UIImagePNGRepresentation(self.imageToUpload.image);

PFFile *file = [PFFile fileWithName:@"image" data:pictureData];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (succeeded) {
        PFObject *imageObject = [PFObject objectWithClassName:@"ReportImageObject"];
        [imageObject setObject:file forKey:@"image"];
        [imageObject setObject:[PFUser currentUser] forKey:@"user"];
        [imageObject setObject:self.descriptionTextField.text forKey:@"comment"];

        [imageObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
            if (succeeded) {
                [self.navigationController popViewControllerAnimated:YES];
            }
            else {
                NSString *errorString = [[error userInfo] objectForKey:@"error"];
                UIAlertController *errorAlertView = [UIAlertController alertControllerWithTitle:@"Error" message:errorString preferredStyle:UIAlertControllerStyleAlert];
                [errorAlertView addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil]];
                [self presentViewController:errorAlertView animated:YES completion:nil];
            }
        }];
    }
    else{
        NSString *errorString = [[error userInfo] objectForKey:@"error"];
        UIAlertController *errorAlertView = [UIAlertController alertControllerWithTitle:@"Error" message:errorString preferredStyle:UIAlertControllerStyleAlert];
        [errorAlertView addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:errorAlertView animated:YES completion:nil];
    }
}progressBlock:^(int percentDone) {
    NSLog(@"Uploaded: %d %%", percentDone);
}];
}

UIViewController,用户在其中分配图像和图像描述 还在创建imageObject的位置

#!/bin/bash
for f in *
do 
newName=File"$(echo "$f" | cut -c4-)"
mv "$f" "$newName"
done

1 个答案:

答案 0 :(得分:1)

只需使用用户名而不是整个用户对象

PFUser *user = (PFUser*)[reportObject objectForKey:KEY_USER];

UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 210, reportImageView.frame.size.width, 15)];
infoLabel.text = [NSString stringWithFormat:@"Uploaded by: %@, %@", user.username, [df stringFromDate:creationDate]];
infoLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:9];
infoLabel.textColor = [UIColor blueColor];
infoLabel.backgroundColor = [UIColor clearColor];
[reportImageView addSubview:infoLabel];


PFObject *imageObject = [PFObject objectWithClassName:@"ReportImageObject"];
imageObject[@"image"] = file;
imageObject[@"user"] = [PFUser currentUser];
imageObject[@"comment"] = self.descriptionTextField.text;