ios - 使用 “IF” 语句弹出到两个可能的 View Controller

标签 ios objective-c uitableview segue

目前我只有一个连接到 UITableView 的 segue,它会弹出到导航堆栈中的下一个 UITableView

我现在需要做的是根据 UITableView 单元格中的文本设置一个 if 语句决定两个可能的UIViewController弹出到哪个

所以 ViewController2 需要能够弹出任何一个”:

- 回到 ViewController1

- 转发到 ViewController3

基于由 JSON 填充的 UITableView 单元格中的文本。

你能帮我解决这个问题吗?将发布所需的任何代码。谢谢!

当前的 ViewController:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"standardCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Spring *spring = [springs objectAtIndex:indexPath.section];
    Leaf *leaf = [spring.leafs objectAtIndex:indexPath.row];
    cell.textLabel.text = leaf.shortName;

    return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"themes"]) {

        UITableViewCell *cell = (UITableViewCell*)sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

        // Get reference to destination view controller
        MRTViewController *tvc = (MRTViewController *)[segue destinationViewController];
    }
}

更新 为每个@troops 添加了代码,但仍然不确定如何弹出到 VC3

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *stringToCheck = @"AAA";
    if([cell.textLabel.text isEqualToString:stringToCheck])
    {
        //Pop to VC1
        [self.navigationController popToViewController:[[self.navigationController viewControllers]objectAtIndex:0] animated:YES];
    } else {
        //Pop to VC3, not sure what to put here


        // Pass the info
        tvc.spring = [springs objectAtIndex:indexPath.section];
        tvc.leaf = [tvc.spring.leafs objectAtIndex:indexPath.row];
        tvc.buttonNumber = _buttonNumber;
    }
}

最佳答案

您可以简单地检查单元格的 textLabel 字符串:如果您真的想使用 segues 而不是 tableView 的委托(delegate)方法:didSelectRowAtIndexPath: 这就是为什么我的回答基于您最初的问题/代码的外观喜欢。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *stringToCheck = @"AAA";

    if ([cell.textLabel.text isEqualToString:stringToCheck])
    {
        // Pop to VC1
        [self.navigationController popToRootViewControllerAnimated:YES];
    } 
    else 
    {
        // Push to VC3
        MRTViewController3 *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"YourID"];

        tvc.spring = [springs objectAtIndex:indexPath.section];
        tvc.leaf = [tvc.spring.leafs objectAtIndex:indexPath.row];
        tvc.buttonNumber = _buttonNumber;

        [self.navigationController pushViewController:tvc animated:YES];
    }
}

关于ios - 使用 “IF” 语句弹出到两个可能的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24337176/

相关文章:

ios - 如何隐藏UICollectionView的滚动条

ios - 使用 UIActionSheetDelegate 执行多个 Segue

iOS 7 : Selecting a UICollectionViewCell will select cells in the UITableView within it

iphone - 将 iPhone 上分组 UITableView 中的选择数量限制为每组一个单元格/行

ios - 屏幕内容仅显示在 iOS 模拟器的屏幕的一部分

ios - 如何在另一个 View 中打开图像?

ios - SKSpriteNode - 不同的 Sprite 但功能相同

ios - UIView - subview 内容不会重新绘制,直到再次布置 subview 。为什么?

iphone - 每个部分具有最大行数的可重新排序的 UITableView

ios - 在 iOS 中创建一个可编辑的 TableView ?