ios - 通过将收藏夹的指针添加到用户来更新 Parse 类

标签 ios pointers parse-platform favorites

我正在尝试向我的应用程序添加一项新功能,该应用程序包含来自世界各地的工作。目前,用户可以点击标签栏按钮并查看可供选择的作业列表。从该表格 View Controller ,用户可以选择通过点击表格 View 单元格来查看作业,这会将他们带到显示多个文本字段和解释作业的 TextView 的详细 View 。

我添加了一个名为“收藏夹”的新 View ,因此如果用户在详细 View 上点击星号,它会将 PFUser 添加到解析中名为“收藏夹”的指针列到该特定作业。

但是,这并没有按计划进行。相反,它在我的解析类中创建了一个新作业,除了指针列之外,所有字段都留空。我的问题是如何将特定用户与他们正在查看的工作相关联,以便稍后在我的应用程序的“收藏夹”选项卡中将其拉起?

我的解析类数据包括以下内容:

"Apply"  (array)
"Clearance"  (string)
"Email" (array)
"Job_Description"  (array)
"Location"  (string)
"POC" (array)
"Phone" (array)
"Position" (string)
"Qualifications"  (array)
"Rotation"  (string)
"Type" (string)
"imageFile" (file)
"createdBy" (pointer<user>)
"Favorites"  (pointer<user>)

#import "JobDetailViewController.h"
#import <Parse/Parse.h>

@interface JobDetailViewController ()

@end

@implementation JobDetailViewController

@synthesize jobPhoto;
@synthesize RotationLabel;
@synthesize QualificationsTextView;
@synthesize Job_DescriptionTextView;
@synthesize TypeLabel;
@synthesize LocationLabel;
@synthesize ClearanceLabel;
@synthesize PositionLabel;
@synthesize job;
@synthesize POC;
@synthesize Email;
@synthesize Phone;
@synthesize Apply;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [_Scroller setScrollEnabled:YES];
    [_Scroller setContentSize:CGSizeMake(320, 2200)];

    [self dismissKeyboard];
    self.PositionLabel.text = job.position;
    self.RotationLabel.text = job.rotation;
    self.LocationLabel.text = job.location;
    self.TypeLabel.text = job.type;
    self.ClearanceLabel.text = job.clearance;
    jobPhoto.file = (PFFile *)job.imageFile;
    [jobPhoto loadInBackground];

    NSMutableString *pocText = [NSMutableString string];
    for (NSString* poc in job.poc) {
        [pocText appendFormat:@"%@\n", poc];
    }
    self.POC.text = pocText;

    NSMutableString *emailText = [NSMutableString string];
    for (NSString* email in job.email) {
        [emailText appendFormat:@"%@\n", email];
    }
    self.Email.text = emailText;


    NSMutableString *phoneText = [NSMutableString string];
    for (NSString* phone in job.phone) {
        [phoneText appendFormat:@"%@\n", phone];
    }
    self.Phone.text = phoneText;

    NSMutableString *applyText = [NSMutableString string];
    for (NSString* apply in job.apply) {
        [applyText appendFormat:@"%@\n", apply];
    }
    self.Apply.text = applyText;

    NSMutableString *qualificationsText = [NSMutableString string];
    for (NSString* qualifications in job.qualifications) {
        [qualificationsText appendFormat:@"%@\n", qualifications];
    }
        self.QualificationsTextView.text = qualificationsText;

    NSMutableString *job_descriptionText = [NSMutableString string];
    for (NSString* job_description in job.job_description) {
        [job_descriptionText appendFormat:@"%@\n", job_description];
    }
    self.Job_DescriptionTextView.text = job_descriptionText;
}

- (void)viewDidUnload
{
    [self setJobPhoto:nil];
    [self setPositionLabel:nil];
    [self setRotationLabel:nil];
    [self setLocationLabel:nil];
    [self setTypeLabel:nil];
    [self setQualificationsTextView:nil];
    [self setJob_DescriptionTextView:nil];
    [self setPOC: nil];
    [self setPhone:nil];
    [self setEmail:nil];
    [self setApply:nil];
    [self dismissKeyboard];

    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

-(void) dismissKeyboard {
    [Email resignFirstResponder];
    [POC resignFirstResponder];
    [Phone resignFirstResponder];
    [Job_DescriptionTextView resignFirstResponder];
    [QualificationsTextView resignFirstResponder];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
 return NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) favoriteSuccess {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Added job to Favorites!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}


- (void) favoriteFailed {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:@"Error occurred while adding to Favorites!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}


- (IBAction)favoriteButtonAction:(id)sender {

    PFObject *objectLike = [PFObject objectWithClassName:@"Jobs"];
    [objectLike setObject:[PFUser currentUser] forKey:@"Favorites"];
    [objectLike saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            [self favoriteSuccess];
        }
        else {
            [self favoriteFailed];
        }
    }];

}
@end
- (IBAction)favoriteButtonAction:(id)sender {
    PFObject *user = [PFObject objectWithClassName:@"User"];
    PFObject *jobs = [PFObject objectWithClassName:@"Jobs"];
    PFRelation *relation = [user relationForKey:@"Favorites"];
    [relation addObject:jobs];
    [user saveInBackground];
}

最佳答案

您可能应该使用从用户到作业的关系,而不是使用指针或指针数组。然后,当点击按钮时,您将收藏添加(或删除)到关系中并保存用户。

您在哪里调用 objectWithClassName您正在创建一个新的 Job 实例,这不是您想要的。您应该引用您下载的实例来填充 UI。但是,如果你这样做了,任何工作都只能被一个用户收藏。此外,除了创建它的人之外,该作业实际上应该对所有人都是只读的。

所以,改为使用关系。

将作业添加到收藏夹的用户列表

PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:@"Favorites"];
[relation addObject:job];
[user saveInBackground];

然后,以后,得到最喜欢的工作
PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:@"Favorites"];
PFQuery *query = [relation query];
[query orderByDescending:@"createdAt"];
query.limit = 200;

[query findObjectsInBackgroundWithBlock:...

关于ios - 通过将收藏夹的指针添加到用户来更新 Parse 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31040926/

相关文章:

ios - 如何在 Swift 中弹出特定的 View Controller

c - 尝试将文件的行扫描到 C 中参差不齐的数组(指针数组)中

C、char** 奇怪的输出

javascript - 解析云码query.withinKilometers

android - 在 ListView 中获取解析推送通知

ios - 在表格 View 页脚中添加按钮

ios - UICollectionViewCell 自动布局安装的 View 不起作用

ios - Cydia应用程序安装问题

c - 正确的分配语法

javascript - 如何在 NodeJS 中正确签署对 Twitter REST API v1.1 的 HTTP 请求?