我创建了 NSObject 的子类,目的是在其他类中使用此类的实例来执行 pdf 文件的创建,然后附加并通过电子邮件发送该文件。这应该避免在整个应用程序中复制繁重的 pdf 创建和电子邮件代码。这是.h文件:
#import <Foundation/Foundation.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface PBVemailWithAttachment : NSObject <MFMailComposeViewControllerDelegate>
//call to create a new PDF file using UIGraphicsBeginPDFContextToFile with the specifeid width and
height
- (void)setupPDFDocumentNamed:(NSString*)name Width:(float)width Height:(float)height;
//call to start a new page in an existing PDF document wit the size provided in pageRect.
- (void)beginPDFPageWithRect: (CGRect)pageRect;
//call to add text to the current pdf page given a specific font and font size and drawn a the given
frame. returns the frame in whcih the text is drawn in order to allow subsequent calls to position
additional elements on the page correctly relvant to the previously added text.
- (CGRect)addText:(NSString*)text withFont:(NSString*)font withFrame:(CGRect)frame fontSize:
(float)fontSize;
//call to draw a line on the page with the given frame and in the given color. Returns the frame of the
drawn line for positioning subsequent objects on the page.
- (CGRect)addLineWithFrame:(CGRect)frame withColor:(UIColor*)color;
//call to place an image on the page at a specific point. A CGRect will be created for the image frame
using the CGpoint and the width and height of the provided image. Returns the frame Rect of the image
to popsition subsequent objects on the page.
- (CGRect)addImage:(UIImage*)image atPoint:(CGPoint)point;
//closes the UIGraphics content being used to generate the PDF file
- (void)finishPDF;
-(void)emailFileNameAsAttachment:(NSString *)filename withSubject: (NSString *)subject withMessageBody:
(NSString *)messageBody withMIMEType: (NSString *)mimeType AttachmentNamed:(NSString *)attachmentName;
@end
问题出在 .m 文件中,我在实现委托方法时遇到错误,说dismissViewControlelrAnimated 不是明显声明的选择器。现在我意识到这是因为我将 NSObject 子类化了。所以我有几个关于如何完成这项工作的问题:
- 我应该继承 UIViewConotrller 还是 NSObject?
可能更重要的是,这可能会回答 1,我在哪里实现 MFMailCompose 委托,在自定义对象上或在将调用此对象的实例并调用其方法来执行创建 PDF 的“繁重工作”的类中和附上ti并通过电子邮件发送出去?
-(void)mailComposeController:(MFMailComposeViewController *)控制器didFinishWithResult:(
MFMailComposeResult)结果错误:(NSError *)错误{[自我dismissViewControllerAnimated:是完成:无];}