【问题标题】:Set delegate to self in a subview. Crash在子视图中将委托设置为自我。碰撞
【发布时间】:2015-07-16 08:42:03
【问题描述】:

是的。我有一个UIViewController,叫做NavigatorViewController。这是一个自定义导航结构,其中包括不同的“插槽”,我可以在其中添加内容并在它们之间滑动 - 这对于实际问题并不重要,但只是为了让您获得代码。 在NavigatorViewController 中添加“插槽”编号 4,如下所示:

slot4 = [self.storyboard instantiateViewControllerWithIdentifier:@"view4"];
slot4.view.frame = CGRectMake(screenWidth*2, 0.0, screenWidth, screenHeight);
[self.view addSubview:slot4.view];

效果很好。我在故事板的正确位置看到了UIViewController (view4)。

在这个 slot4 UIViewController 中,我想添加另一个子视图。另一个 UIViewController 称为 ChatViewController。我用这些行添加它:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ChatViewController *viewController = (ChatViewController *)[storyboard instantiateViewControllerWithIdentifier:@"Chat"];
viewController.view.tag = 266;
[slot4.view addSubview:viewController.view];

到目前为止一切都很好 - 它也可以正常工作。但是..

我的问题是 ChatViewController 内部有一个 UITextView,名为 chatTextView。我设置了这个:

@interface ChatViewController : UIViewController <UITextViewDelegate> (...) 

ChatViewController 标头中,因为我想从chatTextView 获取操作。所以外汇。当chatTextView“成为firstResponder”时,它会调用某种动作。为了实现这一点,我必须设置

chatTextView.delegate = self;

ChatViewControllerviewDidLoad 方法中。

但是当我运行项目并单击chatTextView 时,它崩溃了。 我收到一条错误消息:

Thread 1: EXC_BAD_ACCESS (code=1, address=0xXXXXXXXXX)

当我将chatTextView 的委托设置为nil 时没有错误,但我无法使用它:-)

请问我是否忘记了什么!

【问题讨论】:

  • 发布完整的堆栈跟踪。

标签: ios objective-c iphone delegates


【解决方案1】:

试试这个,而不是仅仅添加 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]; 

使用相同的方法添加 slot4 之类的,

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]; 

【讨论】:

  • 做到了!谢谢:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多