【问题标题】:Switching viewControllers with a UISegmentedControl使用 UISegmentedControl 切换 viewController
【发布时间】:2015-05-26 23:06:54
【问题描述】:

我正在尝试实现此tutorial 中描述的代码,以使用UISegmentedControl 切换UIViewControllers。本教程使用didFinishLaunchingWithOptions: 设置所有内容并显示第一个视图:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSArray * viewControllers = [self segmentViewControllers];

    UINavigationController * navigationController = [[UINavigationController alloc] init];
    self.segmentsController = [[SegmentsController alloc] initWithNavigationController:navigationController viewControllers:viewControllers];

    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[viewControllers arrayByPerformingSelector:@selector(title)]];

    [self.segmentedControl addTarget:self.segmentsController
                          action:@selector(indexDidChangeForSegmentedControl:)
                forControlEvents:UIControlEventValueChanged];

    [self firstUserExperience];

    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

但是,在我的应用程序中,我想在 UITableViewController 中从 didSelectRowAtIndexPath: 调用它,但我不知道该怎么做。因此,用户选择了一行,然后出现一个新视图,我可以使用顶部的UISegmentedControl 在视图之间切换。

我猜需要更改的行是[window addSubview:navigationController.view];,其余的都可以。

如果我从 UITableViewController 而不是教程中的 AppDelegate 调用该代码,那么等效的代码是什么?

(这不是Switching ViewControllers with UISegmentedControl in iOS5 的重复,因为我想使用教程中的示例,而该问题涉及如何使用情节提要进行设置。)

【问题讨论】:

    标签: ios objective-c uinavigationcontroller uisegmentedcontrol


    【解决方案1】:

    虽然我认为这不是一个好的解决方案(我更愿意创建一个单独的视图控制器,使用分段控件,并在那里实现切换逻辑),但答案是是的:你可以从表视图控制器调用这段代码,将导航控制器的视图添加为其子视图,如下所示:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
        NSArray * viewControllers = [self segmentViewControllers];
        
        UINavigationController * navigationController = [[UINavigationController alloc] init];
        self.segmentsController = [[SegmentsController alloc] initWithNavigationController:navigationController viewControllers:viewControllers];
        
        self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[viewControllers arrayByPerformingSelector:@selector(title)]];
        self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        
        [self.segmentedControl addTarget:self.segmentsController
                                  action:@selector(indexDidChangeForSegmentedControl:)
                        forControlEvents:UIControlEventValueChanged];
        [self firstUserExperience];
        [self.view addSubview:navigationController.view];
        
    }
    

    我已经下载了您提到的示例(顺便说一句已经过时了),并将其包含在示例项目中。所以你可以检查一下。您需要做的就是添加一个新项目,然后选择一行来调用相关的代码片段。

    Check the code here

    更新:

    我添加了第二个使用单独视图控制器的示例,如果需要,请查看:Link

    【讨论】:

    • 我喜欢第二种方法。唯一的问题是,我在导航栏下看到了黑光,但这可能是我的代码中的原因,因为我在您的示例中没有看到它。
    • 我只是将示例代码扔到Xcode中的主数据应用程序模板中,所以我并没有真正考虑它。您可以尝试使用内置的视图调试器在运行时检查您的布局。
    • 添加self.navigationController.navigationBar.translucent = NO; 似乎可以解决这个问题。
    【解决方案2】:

    使用容器视图控制器。您所指的教程是在容器视图控制器之前使用的 hack。作者真的应该把那个东西拉下来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 2014-08-22
      • 2012-04-01
      • 2015-09-23
      相关资源
      最近更新 更多