tableview segue到另一个视图控制器

时间:2014-12-01 23:03:52

标签: ios objective-c uitableview parse-platform

我是编程新手,我可能挂了一个简单的问题。我在tableview中使用解析我的数组。当选择行时,我想要转到另一个视图控制器上的搜索栏。 segue工作正常,tableview工作正常,但我似乎无法通过objectId。

#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我总是使用未声明的标识符“post”,但我似乎无法弄清楚如何识别它。这是在上面的方法。

NSLogs正确答复16:17:27.513 [App]单元格点击  [App] cgdVY7Eu9h

3 个答案:

答案 0 :(得分: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;    
    }
}

答案 1 :(得分:1)

Post是您在didSelectRowAtIndexPath中创建的局部变量,因此无法在该方法之外使用。解决这个问题的简单方法是在performSegueWithIdentifier:sender:中将post作为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;    
}

答案 2 :(得分:0)

UIViewController - &gt; segue - &gt;的的UITableViewController

我有一个问题,我用答案解决了-1-谢谢。 所以我有一种UIViewController,我想按钮只是segue到另一个UITableViewController,我注意到它堆叠,并被冻结。我无法滚动我的桌子......

我的代码是:

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

我的CPU超载率超过100%。 所以答案1对我很有帮助。新代码是:

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

并且包含30个条目的表现在就像魅力=)