【问题标题】:Object msg send error coming对象消息发送错误来了
【发布时间】:2011-04-14 13:10:53
【问题描述】:

我正在尝试执行以下操作

- (void)textViewDidChange:(UITextView *)textView{
if(textView == txtYourTip){
    if([txtYourTip.text length] < 100){
        strTip = txtYourTip.text;
        NSLog(@"%@",strTip);
    } else {
        NSLog(@"%@",strTip);
        txtYourTip.text = strTip;
    }}

当控件转到其他部分时应用程序崩溃。 strTip 是 NSString 类型。

viewDidLoad 包含

    txtYourTip = [[UITextView alloc] initWithFrame:CGRectMake(15, 80, 290, 80)];
[txtYourTip.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
[txtYourTip setDelegate:self];
[txtYourTip setContentInset:UIEdgeInsetsMake(10, 10, 10, 10)];
txtYourTip.layer.cornerRadius = 10.0;
[txtYourTip.layer setBorderWidth:2.0];
[contentView addSubview:txtYourTip];

strTip = [[NSMutableString alloc] init];

【问题讨论】:

  • strTip 和 txtYourTip 声明在哪里?你确定它们不是 nil 并且当你到达 else 时它们的指针是有效的吗?
  • 两者都在 .h 文件中声明,并且我在 viewDidLoad 中设置了 strTip = @""
  • 你能发布 viewDidLoad 的源代码吗?还是别的什么?
  • txtYourTip = [[UITextView alloc] initWithFrame:CGRectMake(15, 80, 290, 80)]; [txtYourTip.layer setBorderColor:[[UIColor lightGrayColor] CGColor]]; [txtYourTip setDelegate:self]; [txtYourTip setContentInset:UIEdgeInsetsMake(10, 10, 10, 10)]; txtYourTip.layer.cornerRadius = 10.0; [txtYourTip.layer setBorderWidth:2.0]; [contentView addSubview:txtYourTip]; strTip = [[NSMutableString alloc] init];
  • 也许如果你添加整个班级......但如果你这样做,请更新你的问题 - 不要在 cmets 中添加它。

标签: objective-c nsstring uitextview


【解决方案1】:

如果你像你说的那样把 strTip = @"" 放在 viewDidLoad 中,我认为这就是原因。 使用 @"a text" 分配 NSString 会使其自动释放。所以在某些时候数据被释放,但指针指向它。当您尝试显示它时,您会遇到异常。

在你的 viewDidLoad 中试试这个: strTip = [[NSString alloc] init];

strTip = @"";
[strTip retain];

任何一个都应该工作并且需要一个:

[strTip release];
strTip = nil;

在视图中 viewDidUnload 所以你不会泄漏

【讨论】:

  • 虽然他说的是@"",但你可以从他的代码中看出他实际上是在将[[NSMutableString alloc] init]; 赋值给strTip
  • 我注意到了,但是因为他告诉我们两件不同的事情,以防第二件是使用的。值得一试:)
猜你喜欢
  • 2011-02-24
  • 2012-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多