2017-08-25 7 views
0

私は、ユーザが画像を添付した画像をアップロードできるようにするためにアプリを作った。両方がスクロールビューに表示されます。私はまた、アップロードしたユーザーの詳細とアップロードされた日付を表示する画像の下にUILabelを持っています。代わりに、私は、次を得る:(「ユーザ名、日付によってアップロード」など)..UILabelにPFObjectを割り当てる - PFUserをNSString値にする?

ユーザー情報や日付を表示する必要がありUILabelは私が望むだろう形式でそれをしないん現時点ではしかし

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"]; 

ここで、私はUILabelにPFObjectの値を割り当てています。 ( - 漠然と左上の「テスト3」にアップロードされた画像で見ることができる画像の説明)

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"

として定義されている私を混乱させる事は私の他のラベルがあることですユーザーが入力した内容を正確に表示したdescriptionFieldText.textにアクセスしますか? NSLogPFObject objectForKeyに関連付けてチェックしても、望ましくないUILabelと同様のフォーマットが表示されますが、実際のラベルは希望する値にアクセスしますか? 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); 
} 

のUIViewController場所をユーザ割り当て表示されている -

UPDATED 主な方法は

アップロードされた画像が表示されているビュー・コントローラを使用しています画像と画像の説明 またimageObjectは

- (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); 
}]; 
} 
+0

'[imageObject setObjectメソッドを使用しない理由:[PFUserあるCurrentUser ] .username forKey:@ "user"]; 'または' [imageObject setObject:[PFUser currentUser] .username forKey:@ "username"]; '代わりに? – nathan

+0

「NSString * _strong」の初期化中に、互換性のない型 'NSString * _strong'の初期化エラーが発生しました。 void'' PFObjectが - (void)アクション内の別の.mファイルから来ている可能性があります。 メインの質問を、使用されている2つの主要なメソッドで更新しました – FarisZ

答えて

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; 
+0

問題は、PFObjectのimageObjectが別のviewControllerにあり、ユーザーの個別の送信を格納するためです。 私はPFObject reportObjectを使用しています。これは、他のviewControllerからのすべての送信を格納します。 私は '[imageObject setObject:[PFUser currentUser] .username forKey:@" user "]に推薦しました。 しかし、まだ生成されている "" "' – FarisZ

+1

更新された回答を参照 – nathan

+0

ありがとうございました!更新された答えは働いた:) – FarisZ

関連する問題