我的应用程序中有一个按钮,当按下它时,MFMailComposerViewController将显示,当作曲家解散时,带有自定义视图的MBProgressHUD将显示告诉用户邮件是否成功发送。
如果我按下作曲家中的发送按钮,邮件将被发送,作曲家将被解雇并且HUD将显示,它工作正常。但是,如果我在作曲家视图中按下取消按钮,作曲家会解散,但HUD不显示,并且应用程序崩溃。
以下是崩溃日志。
2012-02-02 22:49:34.821 App[5091:707] -[ViewController size]: unrecognized selector
sent to instance 0x319210
2012-02-02 22:49:34.831 App[5091:707] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[ViewController size]: unrecognized selector sent to instance 0x319210'
*** First throw call stack:
(0x340af8bf 0x342ff1e5 0x340b2acb 0x340b1945 0x340b27b8
0x3748cfa5 0xf051 0x203d1 0x37553f5b 0x374f393b 0x374f37bf
0x3746d81b 0x37472fb9 0x34bc4ba7 0x36ce3e8d 0x340822dd
0x340054dd 0x340053a5 0x30889fcd 0x37486743 0xe7a7 0xe74c)
terminate called throwing an exception
ViewController
是呈现邮件编辑器的控制器。
以下是我使用的一些代码:
-(void)showHUDWithMessage:(NSString *)msg
{
HUD = [[MBProgressHUD alloc]initWithWindow:self.window];
[self.window addSubview:HUD];
HUD.delegate = self;
UIImage *image;
NSString *labelTextToShow;
//Do something here
UIImageView *imageView = [[[UIImageView alloc]initWithImage:image]autorelease];
HUD.labelText = labelTextToShow;
HUD.customView = imageView;
HUD.mode = MBProgressHUDModeCustomView;
[HUD show:YES];
[HUD hide:YES afterDelay:3.0];
}
-(void)mailFriend:(id)sender
{
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc]init];
mailController.mailComposeDelegate = self;
[mailController setSubject:@"Mail Subject"];
NSString *emailBody = @"Message";
[mailController setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:mailController animated:YES];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
NSString *msg;
switch (result) {
case MFMailComposeResultSent:
msg = @"Sent";
break;
case MFMailComposeResultFailed:
msg = @"Fail";
break;
case MFMailComposeResultCancelled:
msg = @"Cancelled";
break;
case MFMailComposeResultSaved:
msg = @"Cancelled";
break;
default:
break;
}
//Show HUD here
[self showHUDWithMessage:msg];
[self dismissModalViewControllerAnimated:YES];
[controller release];
}
因为如果发送邮件,作曲家视图可以成功解散,HUD也可以正确显示,我真的不知道这里有什么问题......
谢谢!
答案 0 :(得分:0)
这很可能是过度释放内存问题。启用NSZombie并测试您的应用。
一些事情: 你在哪里创建了控制器?您有责任释放您拥有的对象。您没有控制器对象,因此不要释放它:
[controller release]; // comment this line
在
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
下一步:
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
NSString *msg = nil; // If result is not equal to any of the case statements then you want to pass nil to [self showHUDWithMessage:msg];
switch (result) {
case MFMailComposeResultSent:
msg = @"Sent";
break;
case MFMailComposeResultFailed:
msg = @"Fail";
break;
case MFMailComposeResultCancelled:
msg = @"Cancelled";
break;
case MFMailComposeResultSaved:
msg = @"Cancelled";
break;
default:
break;
}
//Show HUD here
[self showHUDWithMessage:msg];
[self dismissModalViewControllerAnimated:YES];
[controller release];
}
和MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc]init];
-(void)mailFriend:(id)sender
自动发布或稍后发布。虽然这与您可能遇到的问题无关。
答案 1 :(得分:0)
删除对MBProgressHUD的所有引用,并在您的方法中放入一个简单的NSLog。我打赌我的底部美元MB正在造成这种情况。
如果没有,内存管理是我明显的第二选择。注释掉[控制器发布];