2014-12-01 48 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个条目表的工作现在就像一个魅力=)

相关问题