【问题标题】:Objective c didSelectRowIndexPath - Passing data to other View ControllerObjective c didSelectRowatIndexPath - 将数据传递给另一个视图控制器
【发布时间】:2022-01-13 06:27:50
【问题描述】:

您好,我正在尝试将数据从 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];

          }
        }];}

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

- (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 的单元格时,我想将数据传递给下一个视图控制器 这是我所拥有的,但我从这里失去了。



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

提前致谢

【问题讨论】:

  • UIViewController *vc=[sb instantiateViewControllerWithIdentifier:@"ViewController_Update_Boards"];,这里是vc,你知道它的类吗?所以转换它:MyViewController *vc = (MyViewController *)[sb instatiante...]; 你在那个方法中有indexPath,你知道如何检索boardNo,然后是vc.boardNo = boardNo,或者你想要的任何其他属性。现在,[objNSDictinoaryListOfBoards allKeys],这里的订单不能保证。您应该为您的数据源使用 NSArray 而不是 NSDictionary。
  • 感谢您这么快回答。我不明白我应该怎么做,我对目标 c 很陌生。我不知道如何找回 boardNo
  • @Juan : 和你在 cellForRow 中做的一样
  • @Ptit Xav 谢谢我正在尝试这样,但我在'UIViewController *'类型的对象上找不到属性'boardName'。 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *vc = (UIViewController *)[sb instantiateViewControllerWithIdentifier:@"ViewController_Update_Boards"]; NSString *boardNo =[objNSDictinoaryListOfBoards allKeys][indexPath.row]; NSString *boardName =objNSDictinoaryListOfBoards [boardNo][@"name"]; vc.boardName=boardName;
  • 您需要将 vac 从情节提要中返回到正确的视图控制器类(您使用所需接口定义的那个)

标签: objective-c uitableview uiviewcontroller


【解决方案1】:

假设 Storyboard 中视图控制器的自定义类已设置为 ViewController_Update_Boards,并且该控制器的头文件看起来类似于:

@interface ViewController_Update_Boards: UIViewController

@property (strong, nonatomic) NSString *boardName;

@end

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

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

}

【讨论】:

  • 按照这些步骤现在可以完美运行。很好的解释,我完全理解,我很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 2012-05-02
  • 1970-01-01
相关资源
最近更新 更多