1

我有某種同一件事情的問題,我正在做一個應用,我有一個網頁視圖,顯示一個PDF文件,然後我有一個按鈕,如果點擊就會調用UIDocumentInteractionController(在新窗口中打開PDF)。打開應用程序內部的PDF打破設計

我會留下截圖,使其更容易:

當我打開應用程序:

http://imgur.com/dpIEd

後,我打開UIDocumentInteractionController:

http://imgur.com/VYV2N

下面的代碼太

.h文件中

@interface pdfView : UIViewController <UIDocumentInteractionControllerDelegate> 
{ 
    IBOutlet UIActivityIndicatorView *loading; 
    IBOutlet UIWebView *Wview; 
    UIDocumentInteractionController *controller; 
} 

@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *loading; 

@property (nonatomic, retain) IBOutlet UIWebView *Wview; 

@property (nonatomic, retain) UIDocumentInteractionController *controller; 

-(void)buttonPressed; 
-(IBAction)open_in:(id)sender; 
-(IBAction)back:(id)sender; 

@end 

.m文件

#import "pdfView.h" 


@implementation pdfView 

@synthesize loading,Wview; 
@synthesize controller; 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

- (void)viewDidLoad 
{ 
    [loading startAnimating]; 

    [super viewDidLoad]; 

    Wview.scalesPageToFit = TRUE; 
    Wview.multipleTouchEnabled = TRUE; 
    [Wview setOpaque:NO]; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"guia_seguranca_2" ofType:@"pdf"]; 
    NSURL *targetURL = [NSURL fileURLWithPath:path]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; 
    [Wview loadRequest:request]; 

    [loading stopAnimating]; 
} 

-(IBAction)open_in:(id)sender 
{ 
    NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:@"guia_seguranca_2" ofType:@"pdf"]; 
    UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileToOpen]]; 
    preview.delegate = self; 
    [preview presentPreviewAnimated:YES]; 
    [preview retain]; 
} 

-(IBAction)back:(id)sender 
{ 
    [self.view removeFromSuperview]; 
} 

-(void)buttonPressed 
{ 
    //[self.view removeFromSuperview]; 

    NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:@"guia_seguranca_2" ofType:@"pdf"]; 
    UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileToOpen]]; 
    preview.delegate = self; 
    [preview presentPreviewAnimated:YES]; 
    [preview retain]; 
} 

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller 
{ 
    return self; 
} 

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller 
{ 
    return self.view; 
} 

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller 
{ 
    return self.view.frame; 
} 

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller 
{ 
    [self.controller autorelease]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc. that aren't in use. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
    [Wview release]; 
    [loading release]; 
} 

@end 

在此先感謝.....

+1

我沒有回答你的問題,但我認爲你可以在需要幾個指針編碼慣例。 1.大寫你的類名,即PdfView。當創建一個UIViewController子類,你也應該將其命名爲PdfViewController以避免混淆。 2.不要大寫變量名稱,即wView。 3.(重要)調用[超級的dealloc]你釋放你的財產,而不是之前後。 4.不要在方法名稱中使用下劃線,例如openIn。 5.修復了代碼的縮進會使人們更方便您和他人的幫助。 –

+0

謝謝你的建議,將設法改善這一點,儘快修改的話題。 :) –

回答

0

解決了,添加了這個代碼版圖驗證:

-(void) viewWillAppear:(BOOL)animated 
    { 
    CGRect arect=[[UIScreen mainScreen]applicationFrame]; 

    CGRect anotherrect=[[UIApplication sharedApplication]statusBarFrame]; 

    if(self.view.center.y==arect.size.height/2) 

    self.view.center=CGPointMake(self.view.center.x, self.view.center.y+anotherrect.size.height); // fix silliness in IB that makes view start 20 pixels higher than it should on iPhone 
    } 

來源:https://discussions.apple.com/thread/2658315?start=0&tstart=0

1

我建議不期運用web視圖,並使用PDF格式的應用程序(的iBook,cloudreader ) 喜歡這個。

//name of pdf 
NSString * pathString = @"userguide"; 
//get pdf path 
NSString * filePath = [[NSBundle mainBundle] pathForResource:pathString ofType:@"pdf"]; 

NSURL *url = [NSURL fileURLWithPath:filePath]; 
self.docController = [UIDocumentInteractionController interactionControllerWithURL:url]; 

BOOL isValid = [self.docController presentOpenInMenuFromRect:self.handBookLaunch.frame inView:self.view animated:YES]; 

if (!isValid) { 
    NSString * messageString = [NSString stringWithFormat:@"No PDF reader was found on your device. Please download a PDF reader (eg. iBooks)."]; 

    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:messageString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alertView show];