this article reprinted to http://blog.csdn.net/devday/article/details/6580444Document Iosuinavigationcontrollerextensionmicrosoftcomponents
What is the preview feature of the technical documentation in the iOS 4 SDK? Is the preview feature when you print the file. It is used in quicklook.framework, which supports document formats such as: IWork documents, Microsoft Office, Rich Text format, PDF, images, Text files and comma-s eparated (CSV) files.
Show a demo today showing its usage:
First step: Create a View-based project and join Quicklook.framewrok
Step Two: Modify the controller's header file as follows:
- #import <QuickLook/QuickLook.h>
- @interface Testviewcontroller:uitableviewcontroller <QLPreviewControllerDataSource>
- {
- Nsarray *arrayofdocuments;
- }
- @end
Modify the controller execution file as follows
- #import "TestViewController.h"
- @implementation Testviewcontroller
- #pragma mark-
- #pragma mark Initialization
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- -(ID) init
- {
- if (self = [super init])
- {
- Arrayofdocuments = [[Nsarray alloc] Initwithobjects:
- @ "iosdevtips.png", @ "Remodel.xls", @"Core j2me technology.pdf", nil];
- }
- return self;
- }
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- -(void) Loadview
- {
- [Super Loadview];
- [Self settitle:@"Files Available for Preview"];
- }
- #pragma mark-
- #pragma Mark Table Management
- Customize the number of sections in the table view.
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- -(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
- {
- return 1;
- }
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
- {
- return [arrayofdocuments Count];
- }
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
- {
- static NSString *cellidentifier = @"TableRow";
- UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
- if (cell = = nil)
- cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier] Autorelease];
- // ???
- [[Cell Textlabel] settext:[arrayofdocuments ObjectAtIndex:indexPath.row];
- [Cell Setaccessorytype:uitableviewcellaccessorydisclosureindicator];
- return cell;
- }
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
- {
- //When user taps a row, create the preview controller
- Qlpreviewcontroller *previewer = [[[[Qlpreviewcontroller alloc] init] autorelease];
- //Set data source
- [Previewer setdatasource:self];
- //which item to preview
- [Previewer SetCurrentPreviewItemIndex:indexPath.row];
- //Push new Viewcontroller, previewing the document
- [Self Navigationcontroller] pushviewcontroller:previewer Animated:yes];
- }
- #pragma mark-
- #pragma Mark Preview Controller
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- -(Nsinteger) Numberofpreviewitemsinpreviewcontroller: (Qlpreviewcontroller *) controller
- {
- return [arrayofdocuments Count];
- }
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- -(ID <QLPreviewItem>) Previewcontroller: (Qlpreviewcontroller *) controller previewitematindex: (Nsinteger) Index
- {
- //Break the path into it's components (filename and extension)
- Nsarray *filecomponents = [[arrayofdocuments objectatindex:index] componentsseparatedbystring:@"."];
- //use of the filename (index 0) and the extension (index 1) to get path
- NSString *path = [[NSBundle mainbundle] pathforresource:[filecomponents objectatindex:0] OfType:[fileComponents Objectatindex:1]];
- return [Nsurl Fileurlwithpath:path];
- }
- #pragma mark-
- #pragma Mark Cleanup
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- -(void) Dealloc
- {
- // free up all the documents
- [Arrayofdocuments release];
- [Super Dealloc];
- }
- @end
Modify Appdelegate as follows
- -(void) applicationdidfinishlaunching: (uiapplication *) Application
- {
- //Create and initialize the window
- window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds];
- //Create Test View Controller
- VC = [[Testviewcontroller alloc] init];
- //Create navigation controller
- nav = [[Uinavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:VC];
- [Window Addsubview:[nav view];
- [Window makekeyandvisible];
- }
The required resource files can be found in the source code.
iOS Document preview feature tutorial