Home > front end >  Objective c didSelectRowIndexPath - Passing data to other View Controller
Objective c didSelectRowIndexPath - Passing data to other View Controller

Time:12-17

Hi there i am trying to pass the data from my TableViewController_My_boards to ViewController_Update_Boards but i am not sure how to do it So first of all i create a dictionary with the data from firestore

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    _db = [FIRFirestore firestore];
    
    objNSDictinoaryListOfBoards =[[NSMutableDictionary alloc]initWithCapacity:5];
    [[self.db collectionWithPath:@"boards"]
        getDocumentsWithCompletion:^(FIRQuerySnapshot * _Nullable snapshot,
                                     NSError * _Nullable error) {
          if (error != nil) {
            NSLog(@"Error getting documents: %@", error);
          } else {
            for (FIRDocumentSnapshot *document in snapshot.documents) {
                NSString *key = document.documentID;
                NSDictionary *boardDetails = document.data;
                [self->objNSDictinoaryListOfBoards setObject:boardDetails forKey:key];
                
                //NSLog(@"%@ => %@", document.documentID, document.data);

            }
              NSLog(@"self->objNSDictionaryListOfBoards= %@", self->objNSDictinoaryListOfBoards);
              [self.tableView reloadData];

          }
        }];}

After that i display the name of each item in my table view cells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleIdentifier1" forIndexPath:indexPath];
   
   // Configure the cell...
   NSString *boardNo =[objNSDictinoaryListOfBoards allKeys][indexPath.row];
   NSString *boardName =objNSDictinoaryListOfBoards [boardNo][@"name"];

   cell.textLabel.text=boardName;
   return cell;
}

And then i want to pass the data to next viewcontroller when i click on the cell with the didSelectRowIndexPath This is what i have but im lost from here.



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc=[sb instantiateViewControllerWithIdentifier:@"ViewController_Update_Boards"];
    vc.modalTransitionStyle= UIModalTransitionStyleFlipHorizontal;
    //[self presentViewController:vc animated:YES completion:NULL];
    [self.navigationController pushViewController:vc animated:TRUE];
}

Thanks in advance

CodePudding user response:

Assuming the Custom Class for the view controller in your Storyboard has been set to ViewController_Update_Boards, and you have a header file for that controller that looks similar to this:

@interface ViewController_Update_Boards: UIViewController

@property (strong, nonatomic) NSString *boardName;

@end

Make sure your TableViewController_My_boards class has this at the top:

#import "ViewController_Update_Boards"

then change your didSelectRowAtIndexPath method to this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController_Update_Boards *vc = (ViewController_Update_Boards *)[sb instantiateViewControllerWithIdentifier:@"ViewController_Update_Boards"];
    NSString *boardNo =[objNSDictinoaryListOfBoards allKeys][indexPath.row];
    vc.boardName = objNSDictinoaryListOfBoards[boardNo][@"name"];
    [self.navigationController pushViewController:vc animated:TRUE];

}
  • Related