对象消息发送错误来了
我试图执行以下
- (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];
I am trying to do the following
- (void)textViewDidChange:(UITextView *)textView{
if(textView == txtYourTip){
if([txtYourTip.text length] < 100){
strTip = txtYourTip.text;
NSLog(@"%@",strTip);
} else {
NSLog(@"%@",strTip);
txtYourTip.text = strTip;
}}
Application crashes when controls goes to else part. strTip is of type NSString.
viewDidLoad contains
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你像你说的那样将 strTip = @"" 放在 viewDidLoad 中,我认为这就是原因。
使用 @"a text" 分配 NSString 会使其自动释放。因此,在某个时刻,数据被释放,但指针指向它。当你尝试显示它时,你会遇到异常。
在你的 viewDidLoad 中试试这个:
strTip = [[NSString alloc] init];
或者
其中一个应该可以工作,并且需要
在视图 viewDidUnload 中添加一个 : ,这样就不会泄漏
If you put the strTip = @"" in viewDidLoad like you said I think that is the reason.
Allocating a NSString with @"a text" is making it autoreleased. So at some point the data is released but the pointers points to it. When you try to display it you have an exception.
Try this in your viewDidLoad :
strTip = [[NSString alloc] init];
or
Either one should work and will need a :
in the view viewDidUnload so you don't leak