gpt4 book ai didi

ios - 使用 UISegmentedControl 切换 viewController

转载 作者:行者123 更新时间:2023-12-01 18:11:40 31 4
gpt4 key购买 nike

我正在尝试实现 tutorial 中描述的代码切换UIViewController s 使用 UISegmentedControl .本教程使用didFinishLaunchingWithOptions:设置所有内容并显示第一个 View :

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

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

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

如果我从我的 UITableViewController 调用该代码,那将是什么?而不是来自 AppDelegate和教程一样?

(这不是 Switching ViewControllers with UISegmentedControl in iOS5 的副本,因为我想使用教程中的示例,并且该问题涉及如何使用 Storyboard 进行设置。)

最佳答案

虽然我认为这不是一个好的解决方案(我更喜欢创建一个单独的 View Controller ,使用分段控制,并在那里实现切换逻辑),但答案是 是的 :您可以从 TableView Controller 调用这段代码,将导航 Controller 的 View 添加为其 subview ,如下所示:

- (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
更新:
我添加了第二个使用单独 View Controller 的示例,如果需要,请查看: Link

关于ios - 使用 UISegmentedControl 切换 viewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29734810/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com