提问人:Juan 提问时间:12/8/2021 更新时间:12/9/2021 访问量:61
目标 c didSelectRowIndexPath - 将数据传递到其他视图控制器
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];
}
}];}
之后,我在表视图单元格中显示每个项目的名称
- (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];
}
提前致谢
答:
0赞
DonMag
12/9/2021
#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];
}
评论
0赞
Juan
12/9/2021
按照这些步骤现在可以完美运行。很好的解释,我已经完全理解了,我很感激。
评论
UIViewController *vc=[sb instantiateViewControllerWithIdentifier:@"ViewController_Update_Boards"];
,在这里,你知道它的类吧?所以投射它:你有 在该方法中,您知道如何检索 然后 ,或任何其他您想要的属性。现在,这里不能保证顺序。您应该使用 NSArray 而不是 NSDictionary 作为数据源。vc
MyViewController *vc = (MyViewController *)[sb instatiante...];
indexPath
boardNo
vc.boardNo = boardNo
[objNSDictinoaryListOfBoards allKeys]