Search code examples
iosobjectuinavigationcontrollerparse-platformpushviewcontroller

Parse pushing data to detail view equals null


I'm trying to push data to a detail view. I'm able to push to the new view but the object equals null.

ProfileDetailView.h

@property (nonatomic, strong) PFObject *user;
@property (weak, nonatomic) IBOutlet UILabel *username;
@property (weak, nonatomic) IBOutlet UILabel *bio;
@property (weak, nonatomic) IBOutlet UILabel *name; 

TableViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    ProfileDetialView *controller = [[ProfileDetialView alloc]initWithNibName:@"ProfileDetialView" bundle:Nil];
    controller.user = object;
    //NSLog(@"%@",controller.user);
    [self performSegueWithIdentifier: @"ShowDetails" sender: self];
    }

I've tried doing this multiple ways but ether it won't push to new view or it wont pass the object to the new view. It's probably something stupid as usually but any suggestions would be appreciated!


Solution

  • You should use prepareForSegue.

    Assume ShowDetails segue goes to ProfileDetailView (and it is a controller) (am I right?).

    you should add into TableViewController

    - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([segue.identifier isEqualToString:@"ShowDetails"]) {
            ProfileDetailView *detailController = segue.destinationViewController;
            detailController.user = (PFObject *) sender;
        }
    }
    

    and change :

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        PFObject *userObject = [self.objects objectAtIndex:indexPath.row];
        [self performSegueWithIdentifier: @"ShowDetails" sender: userObject];
    }
    

    You code doesn't work, because you instantiate NEW detail controller, add data to it, but segue opens ANOTHER instance of detail controller (where you have no data).