0

我正在使用 parse.com。我正在尝试显示评论,但为了获得评论,我必须从我的 DetailViewController 获取 PFObject,但它不会将 PFObject 返回到 CommentsViewController

编辑

这是所有代码。我是目标 c 的新手,所以我可能在做一些愚蠢的事情

评论视图控制器

#import "CommentsViewController.h"
#import "DetailViewController.h"
#import "CommentsCell.h"

@interface CommentsViewController (){
    NSArray *commentsArray;
    id commentInstance;
}

@end

@implementation CommentsViewController




- (id)init {
    if (self = [super init]) {
        [self performSelector:@selector(commentsQuery)];
    }
    return self;
}







- (void)commentsQuery {

    commentInstance = [[DetailViewController alloc] init];

    PFObject *tempObj = [commentInstance placeObject];
    PFQuery *query1 = [PFQuery queryWithClassName:@"activity"];
    [query1 whereKey:@"type" equalTo:@"comment"];
    [query1 whereKey:@"place" equalTo:[NSString stringWithFormat:@"%@", [tempObj objectId]]];
    NSLog(@"%@", tempObj);
    [query1 orderByDescending:@"createdAt"];
    [query1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error){
            commentsArray = [[NSArray alloc]initWithArray:objects];
            NSLog(@"%lu", (unsigned long)[commentsArray count]);
        }
    }];
}



-  (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellIdentifier = @"Cell";
    CommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CommentsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    PFObject *placeObj = [commentInstance placeObject];
    cell.username.text = @"test";
    return cell;
}


@end

详细信息视图控制器

#import "DetailViewController.h"
#import "CommentsViewController.h"

@interface DetailViewController (){

    CommentsViewController *test;
}

@end

@implementation DetailViewController

@synthesize place;
@synthesize userPhoto, message, username, checkCount, photo, scroller, commentsTableView;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    test = [[CommentsViewController alloc] init];
    self.commentsTableView.delegate = test;
    self.commentsTableView.dataSource = test;



    //Adjust the message label box height
    CGSize textSize = [[place objectForKey:@"message"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(201, CGFLOAT_MAX) lineBreakMode: NSLineBreakByWordWrapping];




    userPhoto.file = [place objectForKey:@"thumbnail"];
    [userPhoto loadInBackground];

    username.text = [place objectForKey:@"username"];
    message.text = [place objectForKey:@"message"];
    checkCount.text = [NSString stringWithFormat:@"%@", [place objectForKey:@"checkMarkCount"]];
    [message sizeToFit];
    if ([place objectForKey:@"photo"] != Nil){
        photo.file = [place objectForKey:@"photo"];
        [photo loadInBackground];
        //[photo sizeToFit];
        photo.frame = CGRectMake(6, textSize.height + 50, photo.frame.size.width, photo.frame.size.height);

        float sizeOfContent = 0;
        UIView *lLast = [scroller.subviews lastObject];
        NSInteger wd = lLast.frame.origin.y;
        NSInteger ht = lLast.frame.size.height;
        NSLog(@"%@", lLast);

        sizeOfContent = wd+ht;
        scroller.contentSize = CGSizeMake(scroller.frame.size.width, sizeOfContent+100);
    }
    else{

        float sizeOfContent = 0;
        UIView *lLast = [scroller.subviews lastObject];
        NSInteger wd = lLast.frame.origin.y;
        NSInteger ht = lLast.frame.size.height;
        NSLog(@"%@", lLast);

        sizeOfContent = wd+ht;
        scroller.contentSize = CGSizeMake(scroller.frame.size.width, sizeOfContent+100);

    }







}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)checkMarkButton:(UIButton *)sender {
}




- (PFObject *)placeObject{
    return place;
}
@end
4

2 回答 2

0

我摆脱了所有这些,在我的 DetailViewController 中,我在 viewDidLoad 方法中添加了 [test placeSet:place]。然后在 CommentsViewController 我做了...

- (void)placeSet:(PFObject *)object{
    place = object;
    NSLog(@"place = %@", place);
}
于 2013-09-22T04:26:52.270 回答
0

我看不到您在哪里分配place对象。你打电话init,然后尝试检索place。它应该返回零。这是您的代码:

commentInstance = [[DetailViewController alloc] init];
PFObject *tempObj = [commentInstance placeObject];

commentInstance 是一个新对象。DetailViewController 中没有定义 init 函数,因此 commentInstance.place 为 nil,由 placeObject 方法返回。

您将地点定义为属性

@property ... place

这为您提供了一个成员变量 _pace 和两个成员函数,place()以及setPlace(place). 对象本身不是为您分配的。在你的函数中分配 place 对象init,去掉,用callplaceObject()替换它。object.place

于 2013-09-22T03:24:07.793 回答