Modifying QuartzDemo example application
Can anyone please write a simple example on how to pass a number (int value) which gets created in 1 .m file to another .m file.
In the apple demo application called QuartzDemo, there is a file called QuartzImages.m
This file has the following line of code: [CODE]CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);[/CODE] Notice the (pdf, 1) in that line. This number should be replaced with an integer variable.
Now, there is also a file called MainViewController.m. In that file, there is a method? called -(void)viewDidLoad
In that method, I want to assign a number to the integer variable which would replace the damn "1" with whatever numbe开发者_高级运维r I want.
Any help would be greatly appreciated.
Hoping this takes you out of your misery...
The QuartzPDFView class in QuartzImages.m is not actually instantiated in the viewDidLoad of MainViewController. There, it's class identity is only stored in a dictionary/array referenced by the table view that provides the menu.
The actual quartz view objects are kept in QuartzViewController which itself is created in the didSelectRowAtIndexPath method in MainViewController.
When the QuartzViewController is created and pushed onto the navigation controller, the QuartzViewController's viewDidLoad fires which adds self.quartzView as a subview. The getter method for quartzView (above the viewDidLoad of QVC) finally creates the quartz view object.
So to tell the quartz view to load a given page, here's one way to do it (may not be the most elegant):
- Add a pageNumber property to the QuartzPDFView class
- Set this property in the didSelectRowAtIndexPath method in MainViewController
The minor problem is that the quartzView property in QuartzViewController can be different kinds of quartz views (not just QuartzPDFView). So you need to check that it is a QuartzPDFView before trying to set the pageNumber property.
Step 1: Add pageNumber property to QuartzPDFView class:
//QuartzImages.h
@interface QuartzPDFView : QuartzView
{
CGPDFDocumentRef pdf;
int pageNumber;
}
@property (nonatomic, assign) int pageNumber;
-(void)drawInContext:(CGContextRef)context;
@end
//QuartzImages.m
@implementation QuartzPDFView
@synthesize pageNumber;
...
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, pageNumber);
...
Step 2: Set this property in the didSelectRowAtIndexPath method in MainViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
QuartzViewController *targetViewController = [self controllerAtIndexPath:indexPath];
if ([targetViewController.quartzView isKindOfClass:[QuartzPDFView class]])
{
((QuartzPDFView *)targetViewController.quartzView).pageNumber = 1;
//replace 1 with whatever number or variable you want
}
[[self navigationController] pushViewController:targetViewController animated:YES];
}
The pdf in the sample app doesn't seem to have more than one page by the way.
精彩评论