2014-12-01 87 views
2

我是編程新手,很可能掛了一個簡單的問題。我在我的tableview中使用解析我的數組。當選中該行時,我想繼續瀏覽另一個視圖控制器上的搜索欄。 segue工作正常,tableview工作正常,但我似乎無法得到objectId傳遞。tableview segue到另一個視圖控制器

#import "bookmarkViewController.h" 
#import "Parse/Parse.h" 
#import <ParseUI/ParseUI.h> 
#import "ViewController.h" 

@implementation bookmarkViewController 

@synthesize postArray; 


#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc]     
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self  
action:@selector(refreshButtonHandler:)]]; 

} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    if ([PFUser currentUser]) 
     [self refreshButtonHandler:nil]; 
} 

#pragma mark - Button handlers 

- (void)refreshButtonHandler:(id)sender 
{ 
    //Create query for all Post object by the current user 
    PFQuery *postQuery = [PFQuery queryWithClassName:@"Post"]; 
    [postQuery whereKey:@"author" equalTo:[PFUser currentUser]]; 

    // Run the query 
    [postQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if (!error) { 
      //Save results and update the table 
      postArray = objects; 
      [self.tableView reloadData]; 
     } 
    }]; 
} 

#pragma mark - Table view data source 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  
(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return postArray.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView 
dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell with the textContent of the Post as the cell's text label 
    PFObject *post = [postArray objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:[post objectForKey:@"textContent"]]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  
*)indexPath{ 
    NSLog(@"cell tapped"); 
    PFObject *post = [postArray objectAtIndex:indexPath.row]; 
    NSLog(@"%@", post.objectId); 
    [self performSegueWithIdentifier:@"searchBookmark" sender:self]; 



} 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    ViewController *vc = [segue destinationViewController]; 

    vc.labelText = post.objectId;  
    } 
} 


@end 

在vc.label.text我總是使用未聲明的標識符「後」,但我似乎無法找出得到它認可的方式。這是在上面的方法。

的NSLogs正確回答16:17:27.513 [應用]電池輸出 [應用] cgdVY7Eu9h

+0

看到這些目的C](http://stackoverflow.com/a/22761617/3681880)或[Swift Answers](http://stackoverflow.com/a/31936367/3681880) – Suragch 2015-08-11 08:12:50

回答

1

您的didSelectRowAtIndexPath方法和prepareForSegue改成這樣:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ 
    [self performSegueWithIdentifier:@"searchBookmark" sender:self]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

{ 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

    NSLog(@"cell tapped"); 
    PFObject *post = [postArray objectAtIndex:indexPath.row]; 
    NSLog(@"%@", post.objectId); 
    ViewController *vc = [segue destinationViewController]; 

    vc.labelText = post.objectId;  
    } 
} 
+0

'indexPath'從哪裏來? – Levi 2014-12-01 23:12:05

+0

哎呀,對不起。修復了這個問題 – TooManyEduardos 2014-12-01 23:14:05

+0

這對我有效 - 謝謝 - 並且爲了快速響應。 – ClayD 2014-12-02 14:43:38

1

帖子是您創建一個局部變量在didSelectRowAtIndexPath中,因此它不能在該方法之外使用。解決這個問題的簡單方法是在postSegueWithIdentifier中傳遞post作爲sender參數:sender :.你可以傳遞你想要的任何對象作爲發送者。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { 
    NSLog(@"cell tapped"); 
    PFObject *post = [postArray objectAtIndex:indexPath.row]; 
    NSLog(@"%@", post.objectId); 
    [self performSegueWithIdentifier:@"searchBookmark" sender:post]; 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    PFObject *post = (PFObject *)sender; 
    ViewController *vc = [segue destinationViewController]; 

    vc.labelText = post.objectId;  
} 
+0

這不斷拋出一個異常在vc.labelText = post.objectId; – ClayD 2014-12-02 14:42:50

+0

@ClayD,這種方法應該產生與你標記爲正確的答案相同的結果,所以我不明白一個人可以如何工作,另一個人會產生一個錯誤。 – rdelmar 2014-12-02 16:01:18

+0

對不起。我也沒有。我剛貼了一張,然後粘貼另一張,一張丟了一個例外,另一個沒有。我確定它是我做的,而不是你! – ClayD 2014-12-02 16:50:42

0

的UIViewController - >賽格瑞 - >的UITableViewController

我有一個問題,我解決了答案-1-感謝。 所以我有一種UIViewController,我想用按鈕只是延續到另一個UITableViewController,我注意到它堆積並被凍結。我不能滾動我的表...

我的代碼是:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
MasterViewController *controller =segue.destinationViewController; 
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
[self presentViewController:controller animated:YES completion:nil]; 
} 

我的CPU是100%過載。 所以第一個答案對我很好。新代碼,然後:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    MasterViewController *vc = [segue destinationViewController]; 
    } 

,並與30個條目表的工作現在就像一個魅力=)

相關問題