2013-09-22 78 views
0

我正在使用parse.com。我想顯示評論,但爲了得到我需要從我的DetailViewController得到PFObject的意見,但它不PFObject返回CommentsViewController調用另一個類的方法不會返回

編輯

這裏是所有的代碼。我是新來的Objective C的,所以我可能做一些愚蠢的

CommentsView控制器

#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 

DetailsViewCOntroller

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

你有'DetailViewController :: init'函數嗎?它有什麼作用?你的'placeObject'函數是多餘的,它重複了'property place'。你也不需要'合成地點'指令,編譯器就是這樣做的。 – dmitri

回答

0

我沒有看到你分配place對象。您致電init,然後嘗試檢索place。它應該返回零。這裏是你的代碼:

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

commentInstance是一個新的對象。在DetailViewController中沒有定義init函數,因此commentInstance.place爲零,並且由placeObject方法返回。

您定義的地方作爲一個屬性

@property ... place 

這就給了你一個成員變量_pace和兩個成員函數,place()setPlace(place)。該對象本身並未分配給您。 在你的init函數中分配放置對象,擺脫placeObject(),用object.place調用替換它。

+0

placeFor正在從prepareForSegue方法的前一視圖中傳遞 – david2391

+0

沒有代碼將值賦給放置屬性。 – dmitri

+0

我明白你現在說什麼,我不認爲它會起作用。因爲commentInstance = [[DetailViewController alloc] init];正在創建新實例,但DetailViewController通過segue從主頁獲取PFObject位置。所以如果我創建一個DetailViewController的新實例,它永遠不會有pfobject。有什麼辦法,我可以傳遞到CommentsViewController的地方,或者我應該刪除它,並在DetailViewController內創建tableView? – david2391

0

我擺脫了所有這一切,並在我的DetailViewController中添加了[test placeSet:place]在我的viewDidLoad方法中。然後在CommentsViewController中,我做了...

- (void)placeSet:(PFObject *)object{ 
    place = object; 
    NSLog(@"place = %@", place); 
} 
相關問題