gpt4 book ai didi

ios - 当 UITableViewController 的子类包含在 subview 中时,它不会触发 didSelectRowAtIndexPath,我该如何解决这个问题?

转载 作者:行者123 更新时间:2023-11-29 04:44:45 26 4
gpt4 key购买 nike

我有一个使用“addSubView”嵌入到另一个 View 中的 View Controller ,该 View Controller 不捕获事件,我如何确保它能够捕获事件?

背景:我正在尝试嵌套 View ,以便分解由多个开发人员共享的 Storyboard。为了以最少的功能重复实现这一点,我/我们创建了一个 mainStoryboard,其中包含一个选项卡 Controller 和 4 个选项卡,每个选项卡都包含一个 subview ,该 subview 将 UIView(包含在另一个 Storyboard中)加载到自身中。这些 View 的添加方式如下:

//Add sub view
UIStoryboard *board = [UIStoryboard storyboardWithName:@"MessagesStory" bundle:nil];
UIViewController *boardController = [board instantiateInitialViewController];

[self.view addSubview:boardController.view];
boardController.view.frame = CGRectMake(0, 0, 320, 480);

加载的初始 View Controller 是 UITableView 子类,整个过程非常适合渲染表格及其内容到屏幕,我可以与表格交互并选择行,但是 View Controller 上的事件监听器“didSelectRowAtIndexPath”无法触发。

多亏了 NSLog(),我知道它没有触发:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Please kind sirs help a poor string appear in the console.");

}

我知道它与 subview 相关,因为如果我将 subview 作为主视图单独加载,事件监听器就会正常工作。

感谢任何帮助,因为这比预期的时间要长,并且在完成此实现之前我可能会被拉到另一个项目。

最佳答案

我明白了。

这非常简单,但我必须做大量阅读才能找到答案。我所有的搜索都是“addSubView”教程和示例,我什至不知道“addChildViewController”的存在。

无论如何,我相信事情就这么简单:

-(void)viewDidAppear:(BOOL)animated
{
if (firstLaunch) {
firstLaunch = NO;

//Find the view controller in the other storyboard
UIStoryboard *board = [UIStoryboard storyboardWithName:@"MessagesStory" bundle:nil];
UIViewController *boardController = [board instantiateInitialViewController];

//add it as a child view controller (THIS IS WHAT I WAS MISSING)
[self addChildViewController:boardController];

//now it is okay to add the subview
[self.view addSubview:boardController.view];

//trigger this method (also missing this but it will run without it, I assume is good practice)
[boardController didMoveToParentViewController:self];
}

[super viewDidAppear:animated];
}

一旦我知道我想要“addChildViewController”,就很容易找到信息:

How does View Controller Containment work in iOS 5?

Is it wise to "nest" UIViewControllers inside other UIViewControllers like you would UIViews?

关于ios - 当 UITableViewController 的子类包含在 subview 中时,它不会触发 didSelectRowAtIndexPath,我该如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9815044/

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