简体   繁体   English

使用UISegmentedControl切换viewController

[英]Switching viewControllers with a UISegmentedControl

I'm trying to implement the code described in this tutorial to switch UIViewController s using an UISegmentedControl . 我正在尝试实现本教程中描述的代码,以使用UISegmentedControl切换UIViewController The tutorial uses didFinishLaunchingWithOptions: to set everything up and show the first view: 本教程使用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;
}

However, in my app, I'd like to call this from didSelectRowAtIndexPath: inside a UITableViewController , but I'm not sure how to do that. 但是,在我的应用程序中,我想从UITableViewController内的didSelectRowAtIndexPath:调用此方法,但不确定如何执行此操作。 So the user selects a row, and a new view appears where I can switch between views using a UISegmentedControl at the top. 因此,用户选择一行,然后出现一个新视图,在这里可以使用顶部的UISegmentedControl在视图之间进行切换。

I guess the line that needs to change is [window addSubview:navigationController.view]; 我猜需要更改的行是[window addSubview:navigationController.view]; , all the rest can be the same. ,其余所有内容都可以相同。

What would be the equivalent if I call that code from my UITableViewController instead of from the AppDelegate as in the tutorial ? 如果我从UITableViewController而不是教程中的AppDelegate调用该代码,那将是等效的?

(This is not a duplicate of Switching ViewControllers with UISegmentedControl in iOS5 , since I'd like to use the example from the tutorial and that question deals with how to set it up using storyboards.) (这不是在iOS5中使用UISegmentedControl切换ViewControllers的副本,因为我想使用本教程中的示例,而该问题涉及如何使用情节提要进行设置。)

While I don't think this is a nice solution (I would prefer creating a separate view controller, with the segmented control, and implement the switching logic there), the answer is yes : you can call this piece of code from your table view controller, adding the navigation controller's view as its subview, like so: 虽然我认为这不是一个很好的解决方案(我宁愿创建带有分段控件的单独的视图控制器,并在其中实现切换逻辑),但答案是肯定的 :您可以从表视图中调用这段代码控制器,将导航控制器的视图添加为其子视图,如下所示:

- (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];

}

I have downloaded the example you referred to (quite outdated by the way), and included it in a sample project. 我已经下载了您所引用的示例(已经过时了),并将其包含在示例项目中。 so you can check it out. 这样您就可以检查出来。 All you need to do is add a new item, then select a row to invoke the relevant code piece. 您需要做的就是添加一个新项,然后选择一行以调用相关代码段。

Check the code here 在这里检查代码

UPDATE: 更新:

I have added a second example which uses a separate view controller, check it out if you want: Link 我添加了第二个示例,该示例使用单独的视图控制器,如果需要,请检查出来: 链接

Use the container view controller. 使用容器视图控制器。 The tutorial you're referring to was a hack used prior to container view controllers. 您所指的教程是在容器视图控制器之前使用的一种技巧。 The author should really pull that thing down. 作者真的应该把那件事拖下去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM