场景,为了 DRY,我创建了一个视图控制器(例如 GeneralDelegateForEmailAndSMS),实现协议 MFMailComposeViewControllerDelegate 的方法。
// GeneralDelegateForEmailAndSMS.h
#import <MessageUI/MessageUI.h>
@interface GeneralDelegateForEmailAndSMS : UIViewController <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
@end
// GeneralDelegateForEmailAndSMS.m
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{ ... }
然后在负责邮件的类的类方法(例如 composeDiscountInfo )中(例如 MyMailer)
+(void) composeDiscountInfo:(ResInfoData *)resturantInfo{
// XmsMFMailComposeViewController, a subclass of MFMailComposeViewController in order to overwrite the default auto rotation.
XmsMFMailComposeViewController *picker = [[XmsMFMailComposeViewController alloc] init];
// Q: method returneds an Object with a +1 retain count.
picker.mailComposeDelegate = [[GeneralDelegateForEmailAndSMS alloc] init];
// Q: object leaked, allocated object is not referenced later in the execution path and has a retain count of +1 A:
[picker setSubject:@"title"];
// ... message body construction... and present via AppDelegate.navController
}
我的问题是如何解决潜在的内存泄漏(上面代码注释中提到)?我无法将委托设置为自动释放,因为它看起来会被垃圾收集并使应用程序崩溃。此外,MyMailer 应该只包含类方法,没有处理对象的生命周期方法。解决方案是什么?
我对配置文件指令知之甚少,关于如何使用它来检测泄漏的任何提示?
更新:
MyMailer 应该是“仅类方法”类,在许多需要撰写/发送应用内电子邮件/SMS 的视图控制器中,我可以编写类似“[MyMailer composeMailWith:data]”之类的代码,实现代码如下(即composeDiscountInfo:data 在第二个代码片段)。
在这些类方法中,我希望我可以将 MFMailCompseViewController 的委托属性(mailComposeDelegate)设置为实现 MFMailComposeViewControllerDelegate 的独立类。
也许我应该让 Mailer 成为一个单身人士,在许多 VC 中调用看起来像 '[MyMailer instance] composeThatMailWith:data'。我明天试试!