Objective c didSelectRowatIndexPath - 将数据传递给另一个视图控制器

Objective c didSelectRowIndexPath - Passing data to other View Controller

你好,我正在尝试将数据从我的 TableViewController_My_boards 传递到 ViewController_Update_Boards,但我不知道该怎么做 所以首先我用来自 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];

          }
        }];}

之后我在 table 视图单元格中显示每个项目的名称

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

然后我想在单击带有 didSelectRowIndexPath 的单元格时将数据传递给下一个 viewcontroller 这就是我所拥有的,但我从这里丢失了。



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

提前致谢

假设您的 Storyboard 中视图控制器的自定义 Class 已设置为 ViewController_Update_Boards,并且您有一个类似于此的控制器头文件:

@interface ViewController_Update_Boards: UIViewController

@property (strong, nonatomic) NSString *boardName;

@end

确保您的 TableViewController_My_boards class 在顶部有这个:

#import "ViewController_Update_Boards"

然后将您的 didSelectRowAtIndexPath 方法更改为:

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

}