iOS [Obj-C] segue 基于同一数据库中不同 MySQL 表中的匹配键

iOS [Obj-C] segue based on matching keys in different MySQL tables in same DB

CatViewController.m :

categoryArray = [[NSMutableArray alloc] init];

for(int i = 0; i < jsonArray2.count; i++)
{
    NSString * cID = [[jsonArray2 objectAtIndex:i] objectForKey:@"categoryID"];
    NSString * cDesc = [[jsonArray2 objectAtIndex:i] objectForKey:@"categoryDesc"];
    NSString * cName = [[jsonArray2 objectAtIndex:i] objectForKey:@"categoryName"];

    [categoryArray addObject:[[Categories alloc]initWidthCategoryDesc:cDesc andcategoryName:cName andcategoryID:cID]];
}

[self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"catToTopic" sender:(self)];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"catToTopic"]) {
        TopicViewController *topViewController = [segue destinationViewController];
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        topViewController.data = [self.dataController objectInListAtIndex:indexPath.row];
    }
}

TopicViewController.m:

topicArray = [[NSMutableArray alloc] init];

for(int i = 0; i < jsonArray3.count; i++)
{
    NSString * rID = [[jsonArray3 objectAtIndex:i] objectForKey:@"replyID"];
    NSString * rTopic = [[jsonArray3 objectAtIndex:i] objectForKey:@"replyTopic"];
    NSString * rDate = [[jsonArray3 objectAtIndex:i] objectForKey:@"replyDate"];
    NSString * rContent = [[jsonArray3 objectAtIndex:i] objectForKey:@"replyContent"];
    NSString * rBy = [[jsonArray3 objectAtIndex:i] objectForKey:@"replyBy"];

    [topicArray addObject:[[Posts alloc]initWidthReplyBy: rBy andreplyDate: rDate andreplyTopic: rTopic andreplyContent: rContent andreplyID: rID]];
}

[self.tableView reloadData];
}

所以我遇到的问题是,我不想在 CatViewController 中单击一行并进入新视图并填充 TopicViewController 中的每个回复,而是想将 categoryID 与 replyTopic 匹配并仅填充匹配的条目

分层数据传递让我很头疼!我什至不是在寻找讲义,只是一个我可以很好地阅读并抢先了解该主题的地方!

尝试将 CatViewController 中的 *data 设置为 catID 无效,即使它成功了 - 我无法弄清楚如何在 TopicViewController 中使用它来执行子查询以填充信息...

好的 - 这是一个快速响应,可以帮助您入门。

在你的TopicViewController.h(header)中添加:

@property (nonatomic, retain) NSString * rID;
@property (nonatomic, retain) NSString * rTopic;
@property (nonatomic, retain) NSString * rDate;
@property (nonatomic, retain) NSString * rContent;
@property (nonatomic, retain) NSString * rBy;

将您的 CatViewController.m prepareForSegue 方法更改为:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"catToTopic"]) {
        TopicViewController *topViewController = [segue destinationViewController];
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        topViewController.rID = [[self.dataController objectInListAtIndex:indexPath.row] objectForKey:@"replyID"];
        topViewController.rTopic = [[self.dataController objectInListAtIndex:indexPath.row] objectForKey:@"replyTopic"];
        topViewController.rDate = [[self.dataController objectInListAtIndex:indexPath.row] objectForKey:@"replyDate"];
        topViewController.rComment = [[self.dataController objectInListAtIndex:indexPath.row] objectForKey:@"replyComment"];
        topViewController.rBy = [[self.dataController objectInListAtIndex:indexPath.row] objectForKey:@"replyBy"];
    }
}

然后更改 TopicViewController.m viewDidLoad(或 viewDidAppear,无论您有什么),调用:

[self.tableView reloadData];

===

根据您的意见,我认为您想做的是:

TopicViewController.h:

@property (nonatomic, retain) NSDictionary *theData;

prepareForSegue:

topViewController.theData = [jsonArray2 objectAtIndex:indexPath.row];