在子视图中将委托设置为自身。碰撞
Set delegate to self in a subview. Crash
是的。我有一个名为 NavigatorViewController
的 UIViewController
。这是一个自定义导航结构,其中包括不同的 "slots",我可以在其中添加内容并在它们之间滑动 - 这对于实际问题并不重要,只是为了让您获得代码。
"Slot" 数字 4 添加到 NavigatorViewController
中,如下所示:
slot4 = [self.storyboard instantiateViewControllerWithIdentifier:@"view4"];
slot4.view.frame = CGRectMake(screenWidth*2, 0.0, screenWidth, screenHeight);
[self.view addSubview:slot4.view];
效果很好。我在情节提要中的正确位置看到了 UIViewController
(view4
)。
我想在这个 slot4 UIViewController 中添加另一个子视图。另一个名为 ChatViewController
的 UIViewController。我用这些行添加它:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ChatViewController *viewController = (ChatViewController *)[storyboard instantiateViewControllerWithIdentifier:@"Chat"];
viewController.view.tag = 266;
[slot4.view addSubview:viewController.view];
到目前为止一切顺利 - 它也可以正常工作。但是..
我的问题是在 ChatViewController 内部有一个名为 chatTextView
的 UITextView。我已经设置了这个:
@interface ChatViewController : UIViewController <UITextViewDelegate> (...)
在ChatViewController
header中,因为我想从chatTextView
获取动作。所以外汇。当chatTextView
"becomes firstResponder"时,它会调用某种动作。为此,我必须设置
chatTextView.delegate = self;
在 ChatViewController
的 viewDidLoad
方法中。
但是当我 运行 项目并单击 chatTextView
时,它崩溃了。
我收到一条错误消息:
Thread 1: EXC_BAD_ACCESS (code=1, address=0xXXXXXXXXX)
当我将 chatTextView
的委托设置为 nil
时没有错误,但我无法使用它:-)
请问我是否忘记了什么!
不要只添加 ChatViewController
视图作为子视图试试这个,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ChatViewController *viewController = (ChatViewController *)[storyboard instantiateViewControllerWithIdentifier:@"Chat"];
viewController.view.tag = 266;
[slot4 addChildViewController:viewController];
[slot4.view addSubview:viewController.view];
[viewController didMoveToParentViewController:slot4];
使用相同的方法添加插槽 4,例如,
slot4 = [self.storyboard instantiateViewControllerWithIdentifier:@"view4"];
slot4.view.frame = CGRectMake(screenWidth*2, 0.0, screenWidth, screenHeight);
[self addChildViewController:slot4];
[self.view addSubview:slot4.view];
[slot4 didMoveToParentViewController:self];
是的。我有一个名为 NavigatorViewController
的 UIViewController
。这是一个自定义导航结构,其中包括不同的 "slots",我可以在其中添加内容并在它们之间滑动 - 这对于实际问题并不重要,只是为了让您获得代码。
"Slot" 数字 4 添加到 NavigatorViewController
中,如下所示:
slot4 = [self.storyboard instantiateViewControllerWithIdentifier:@"view4"];
slot4.view.frame = CGRectMake(screenWidth*2, 0.0, screenWidth, screenHeight);
[self.view addSubview:slot4.view];
效果很好。我在情节提要中的正确位置看到了 UIViewController
(view4
)。
我想在这个 slot4 UIViewController 中添加另一个子视图。另一个名为 ChatViewController
的 UIViewController。我用这些行添加它:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ChatViewController *viewController = (ChatViewController *)[storyboard instantiateViewControllerWithIdentifier:@"Chat"];
viewController.view.tag = 266;
[slot4.view addSubview:viewController.view];
到目前为止一切顺利 - 它也可以正常工作。但是..
我的问题是在 ChatViewController 内部有一个名为 chatTextView
的 UITextView。我已经设置了这个:
@interface ChatViewController : UIViewController <UITextViewDelegate> (...)
在ChatViewController
header中,因为我想从chatTextView
获取动作。所以外汇。当chatTextView
"becomes firstResponder"时,它会调用某种动作。为此,我必须设置
chatTextView.delegate = self;
在 ChatViewController
的 viewDidLoad
方法中。
但是当我 运行 项目并单击 chatTextView
时,它崩溃了。
我收到一条错误消息:
Thread 1: EXC_BAD_ACCESS (code=1, address=0xXXXXXXXXX)
当我将 chatTextView
的委托设置为 nil
时没有错误,但我无法使用它:-)
请问我是否忘记了什么!
不要只添加 ChatViewController
视图作为子视图试试这个,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ChatViewController *viewController = (ChatViewController *)[storyboard instantiateViewControllerWithIdentifier:@"Chat"];
viewController.view.tag = 266;
[slot4 addChildViewController:viewController];
[slot4.view addSubview:viewController.view];
[viewController didMoveToParentViewController:slot4];
使用相同的方法添加插槽 4,例如,
slot4 = [self.storyboard instantiateViewControllerWithIdentifier:@"view4"];
slot4.view.frame = CGRectMake(screenWidth*2, 0.0, screenWidth, screenHeight);
[self addChildViewController:slot4];
[self.view addSubview:slot4.view];
[slot4 didMoveToParentViewController:self];