【问题标题】:How to put an UITableViewController in UIView to make touchesBegan to work in iPhone?如何在 UIView 中放置一个 UITableViewController 使 touchesBegan 在 iPhone 中工作?
【发布时间】:2011-07-18 12:28:58
【问题描述】:

我从另一个 UITableViewController 类(比如 Table1)创建并呈现一个 UITableViewController(比如 Table2)类,就像这样..

-(void)createTable2 {
     Table2Controller *table2Controller = [ [Table2Controller alloc] initWithStyle:UITableViewStyleGrouped];
     UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:table2Controller];
     [self.navigationController presentModalViewController:nav animated:YES];
     [nav release];
     [table2Controller release];
}

所以 Table2 将显示出来。我想使用 touchesBegan 方法在 Table2 中退出键盘,因为我有一些 UITextField 作为单元格。我在 Table2 的 .h 文件中包含了 UITextFieldDelegate 协议。

但我知道这些 touchesBegan 方法仅适用于 UIView 而不适用于 UIViewController(对吗?)。但是我不知道在哪里以及如何(我在 createTable2 函数本身中尝试过。它不起作用)添加一个 UIView,然后在该 UIView 中添加 Table2 以使事情发生......任何建议......

【问题讨论】:

  • 在单元格中添加 UItextfield 时,只需添加 txt.delegate=self;然后覆盖 UITextFieldDelegate 方法。你可以使用 -touchesBegan:withEvent:)
  • 对不起,我也忘了添加这个。我已经添加了你的线路。但它没有得到调用
  • 哪个方法没有被调用...UITextFieldDelegate 方法或 touchesBegan
  • 您为什么不为此目的使用 tableview 委托?

标签: iphone uiview uinavigationcontroller uitableview touchesbegan


【解决方案1】:

您的表格视图控制器有一个表格视图属性。您可以子类化表格视图,然后覆盖诸如-touchesBegan:withEvent: 之类的方法。实例化您的自定义表格视图并将此实例设置为视图属性。

【讨论】:

【解决方案2】:

UIViewController 控制屏幕上呈现的 UIView 和其他 UI 元素。由于它们是子类的 UIResponder 类,所有这些元素都可以响应触摸。

使用 Table2Controller 覆盖 touchesBegan 方法来控制在此 viewController 内的任何 UI 元素上发生的触摸事件。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        [inputTV resignFirstResponder];
    [super touchesBegan:touches withEvent:event];
}

注意始终调用超级方法声明,以便您的触摸可以沿响应链向上传播。

【讨论】:

  • 感谢您的详细回复。我只是按照上述方式进行。实际上我的 Table2 类有 UITextField 和一个 UITextView。这会在调用 touchesBegan 方法时产生任何问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 2020-03-18
  • 1970-01-01
  • 2016-07-21
相关资源
最近更新 更多