如何判断一个对象是否已被释放?
如果 UITableViewCell 已移出屏幕,kkcell 对象将由 UITableView 自动释放。
当 audioPlayer 结束时,程序调用“[kkcell stopSpeakAmination]”,但这会导致程序崩溃。
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[kkcell stopSpeakAmination];
playing=NO;
}
我也用
if(kkcell)
或
if(kkcell!=nil)
但它仍然崩溃。
这是我正在使用的更多代码:
//KKMessageCell2.m
....
@property(nonatomic, retain) UIImageView *SenderVoiceNodePlaying;
@property(nonatomic, retain) UIImageView *ReceiverVoiceNodePlaying;
....
-(void)stopSpeakAmination{
[self.SenderVoiceNodePlaying stopAnimating];
[self.ReceiverVoiceNodePlaying stopAnimating];
}
-(void)speakAminationOnRight:(UIButton*)btn{
//rightside
NSArray *speekImageAry = [[NSArray alloc]initWithObjects:
[UIImage imageNamed:@"SenderVoiceNodePlaying001"],
[UIImage imageNamed:@"SenderVoiceNodePlaying002"],
[UIImage imageNamed:@"SenderVoiceNodePlaying003"], nil];
self.SenderVoiceNodePlaying.animationImages = speekImageAry;
self.SenderVoiceNodePlaying.animationDuration = 1.0;
self.SenderVoiceNodePlaying.animationRepeatCount = 0;
[self.SenderVoiceNodePlaying startAnimating];
NSString *fileName=btn.titleLabel.text;
fileName=[fileName substringFromIndex:10];
KKMessageCell2 *cell=(KKMessageCell2*)[btn superview];
[[NSNotificationCenter defaultCenter]postNotificationName:@"nPlayAmr"
object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
fileName,@"fileName",
cell,@"cell",nil]];
}
//KKMessageCell2.m
....
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"msgCell";
NSDictionary *dict = [messages objectAtIndex:indexPath.row];
KKMessageCell2 *cell = [[[KKMessageCell2 alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setCellView:dict andMy_avatar_url:self.my_avatar_url];
[cell.avatarLeftImageButton addTarget:self action:@selector(pushToUserDetail:) forControlEvents:UIControlEventTouchUpInside];
[cell.avatarRightImageButton addTarget:self action:@selector(pushToUserDetail:) forControlEvents:UIControlEventTouchUpInside];
[cell.chatphoto addTarget:self action:@selector(photoClick:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[kkcell stopSpeakAmination];
playing=NO;
}
最佳答案
将您的项目转换为 ARC。它比您想象的要容易和自动化得多。我已经转换了大约 3 个现实生活中的项目,这些项目在开发中期相当大并且从来没有任何问题(留出一个小时来做,你可能不需要那么多时间,这绝对值得)
在 Xcode 中,选择 编辑->重构->转换为 Objective-C ARC
您也可以在线找到教程,但总体来说非常简单。
关于ios - 如何判断一个对象是否已经被释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18306076/