ClayD
ClayD

Reputation: 71

tableview segue to another view controller

I am new to programming and am probably hung up on a simple problem. I am using parse for my array in my tableview. When the row is selected i want to segue to a search bar on another view controller. The segue works fine and the tableview works fine but i can't seem to get the objectId to pass.

#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

at vc.label.text i always get use of undeclared identifier "post" but i can't seem to figure out how to get it recognized. It is in the above method.

the NSLogs reply correctly 16:17:27.513 [App] cell tapped [App] cgdVY7Eu9h

Upvotes: 2

Views: 2557

Answers (3)

Imeksbank
Imeksbank

Reputation: 114

UIViewController -> segue -> UITableViewController

I had one problem that i solved with answer -1- Thanks. So i had a kind of UIViewController and i wanted with button just segue to another UITableViewController and i noticed that it stacked and was frozen. I could not scroll my Table ...

My Code was :

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

My CPU was over 100% overloaded. So the answer number 1 worked for me well. New Code is then :

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

and the table with 30 entries works now just like a charm =)

Upvotes: 0

rdelmar
rdelmar

Reputation: 104082

Post is a local variable that you created inside didSelectRowAtIndexPath, so it can't be used outside that method. The easy way to fix this, is to pass post as the sender argument in performSegueWithIdentifier:sender:. You can pass any object you want as the 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;    
}

Upvotes: 1

TooManyEduardos
TooManyEduardos

Reputation: 4404

Change your didSelectRowAtIndexPath and prepareForSegue to this:

- (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;    
    }
}

Upvotes: 2

Related Questions