我正在 UITextViewDelegate 中处理代码。那里的每个函数都会接收接收事件的 UITextView 实例,例如:

- (void)textViewDidBeginEditing:(UITextView*)textView
{

}

然而,在该方法中,仅给定 textView,我需要访问包含 textView 的 UIViewController。

使用 textView.superview (甚至它的多个阶段)似乎不起作用 - 我只得到:

  • textView.superview = UIView 的一个实例
  • textView.superview.superview = UIViewControllerWrapperView
  • textView.superview.superview.superview = UINavigationTransitionView
  • textView.superview.superview.superview.superview = UILayoutContainerView
  • textView.superview.superview.superview.superview.superview = UIWindow
  • textView.superview.superview.superview.superview.superview.superview = nil

我通过打印类似的内容找到了类名

printf( "class: %s\n", [[[uiv class] description] UTF8String] ) ;
有帮助吗?

解决方案

没有办法做到这一点。UIViewController 知道它管理什么 UIView,但视图没有对其视图控制器的引用(甚至可能没有视图控制器)。

-superview 方法返回另一个 UIView,它与 UIViewController 不同。

为什么需要视图控制器?你想达到什么目的?

顺便说一句,您可以使用 NSLog 而不是 printf 来节省一些击键次数:

NSLog(@"class: %@", [uiv class]); 

其他提示

出于好奇,为什么你的视图控制器不充当 UITextViewDelegate ?在我见过的大多数代码(包括我自己的代码)中,视图控制器本身往往充当委托。

如果您需要将此功能提取到一个单独的类中,也许视图控制器可以将对自身的引用传递给 UITextViewDelegate。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top