【问题标题】:handling of TableViewController, possible to go to either of two controllers with one being a UIWebViewController处理表视图控制器,可以转到两个控制器中的任何一个,其中一个是 UIWebView 控制器
【发布时间】:2012-03-16 19:25:31
【问题描述】:

iOS 开发新手。这只是一个可行性问题。我想看看是否有可能有一个 TableViewController,并根据推入单元格的数据类型,让它在两个不同的控制器之间执行一个 pushViewController。

例如,我们有可以作为搜索结果返回的位置和人员,我们想要使用 ProfileController 或 LocationController 执行 pushViewController?如果其中一个控制器是 UIWebView 是否重要?

谢谢

编辑 1 我想我正在寻找一种方法来判断 TableView 单元格是否响应类似于此问题 In Objective-C, given an id, how can I tell what type of object it points to? 的特定消息并确定要推送哪个 UIViewController。

【问题讨论】:

    标签: ios


    【解决方案1】:

    如果你想“推送”另一个评论控制器,你的表格视图控制器应该嵌入到 UINavigationController 中。然后,有两种方法可以在选择一行时处理新视图控制器:

    如果没有故事板(iOS -tableView:didSelectRowAtIndexPath:。从那里,你可以根据传入的indexPath 选择去哪里,创建一个你想去的视图控制器的实例,然后将它推送到导航控制器上。例如:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UIViewController *nextVC;
        if (indexPath.section == 0) {
            nextVC = [[ProfileController alloc] init];
            // configure nextVC as needed
        } else {
            nextVC = [[LocationController alloc] init];
            // configure nextVC as needed
        }
        [self.navigationController pushViewController:nextVC animated:YES];
    }
    

    使用故事板(iOS 5.0+),通常的做法是将 IB 中的 segue 从表格单元格拖到下一个视图控制器,并根据 @ 中的选择实现您的逻辑来配置目标视图控制器987654324@... 但您不能从同一个表格单元格中拖动多个 segue。取而代之的是,从视图控制器本身拖动 segue,并给它们标识符。像以前一样在-tableView:didSelectRowAtIndexPath: 中做出选择,但调用performSegueWithIdentifier: 而不是自己创建目标视图控制器。例如:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.section == 0) {
            [self peformSegueWithIdentifier:@"showProfile"];
        } else {
            [self peformSegueWithIdentifier:@"showLocation"];
        }
    }
    

    如果您需要根据点击的表格行来配置目标视图控制器,请在-prepareForSegue:sender: 中进行。

    没有 UIWebViewController... 但无论您的一个视图控制器是否管理 UIWebView,这都是一样的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多