Have initwithPDFAtPath in AViewController
-(id)initWithPDFAtPath:(NSString *)path {
NSURL *pdfUrl = [NSURL fileURLWithPath:path];
thePDF = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);
totalPages = (int)CGPDFDocumentGetNumberOfPages(thePDF);
self = [super initWithNibName:nil bundle:nil];
return self;
}
Trying to open pdf file in BViewController using viewDidLoad method
- (void)viewDidLoad {
[super viewDidLoad];
//modelArray holds the page numbers
modelArray = [[NSMutableArray alloc] init];
for (int index = 1; index <= totalPages; index++) {
[modelArray addObject:[NSString stringWithFormat:#"%i", index]];
}
thePageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options:nil];
thePageViewController.delegate = self;
thePageViewController.dataSource = self;
thePageViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSString *path = [[NSBundle mainBundle] pathForResource:#"pages" ofType:#"pdf"];
NSURL *pdfurl = [NSURL fileURLWithPath:path];
PDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfurl);
AViewController = [[AViewController alloc] initWithPDFAtPath:path];
aViewController.page = [modelArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:aViewController];
}
When i execute it crashes with empty array message. Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
EDIT:
AViewController has a PDFScrollview
- (void)viewDidLoad
{
[super viewDidLoad];
// Create our PDFScrollView and add it to the view controller.
CGPDFPageRef PDFPage = CGPDFDocumentGetPage(thePDF, [_page intValue]);
pdfScrollView = [[PDFScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 925)];
[pdfScrollView setPDFPage:PDFPage];
[self.view addSubview:pdfScrollView];
}
From what I can see here, it looks like you initialize modelArray before you have a number for totalPages, since this is only set in initWithPDFAtPath. Although I think you can (and should) reorganize the code so that only one of the view controllers has to manage the page numbers, you can probably fix the issue by moving the line
AViewController *aViewController = [[AViewController alloc] initWithPDFAtPath:path];
to before the point where you initialize modelArray.
Hope this helps!
It sounds to me that totalPages could not be initialised in BViewController.
Also Not sure how you store it, as in AViewController you do initialisation of this variable before calling super init which could lead to loosing this value.
To fix this I think you need do couple of things.
First in AViewContoller change init method to something like this:
-(id)initWithPDFAtPath:(NSString *)path {
self = [super initWithNibName:nil bundle:nil];
if (self) {
NSURL *pdfUrl = [NSURL fileURLWithPath:path];
_thePDF = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);
_totalPages = (int)CGPDFDocumentGetNumberOfPages(thePDF);
}
return self;
}
and store totalPages in parameters.
Than in BViewController you need to do
- (void)viewDidLoad {
[super viewDidLoad];
//modelArray holds the page numbers
thePageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options:nil];
thePageViewController.delegate = self;
thePageViewController.dataSource = self;
thePageViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSString *path = [[NSBundle mainBundle] pathForResource:#"pages" ofType:#"pdf"];
NSURL *pdfurl = [NSURL fileURLWithPath:path];
PDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfurl);
AViewController = [[AViewController alloc] initWithPDFAtPath:path];
modelArray = [[NSMutableArray alloc] init];
if (aViewController.totalPages > 0) {
for (int index = 1; index <= aViewController.totalPages; index++) {
[modelArray addObject:[NSString stringWithFormat:#"%i", index]];
}
aViewController.page = [modelArray objectAtIndex:0];
}
NSArray *viewControllers = [NSArray arrayWithObject:aViewController];
}
But really that seems some obvious problems with all Controllers Architecture.
Related
This is the static navigation bar how i set in my project , but want to do in dynamic way, here is the two type of code...
viewsArray = [[NSArray alloc] init];
AfterloginViewController *toolsnavigation = [[AfterloginViewController alloc] init];
toolsnavigation.tabBarItem.image = [UIImage imageNamed:#"cool.png"];
[toolsnavigation setTitle:#"Tools"];
UINavigationController *nav0 = [[UINavigationController alloc] initWithRootViewController:toolsnavigation];
MapViewController *myridenavigation = [[MapViewController alloc] init];
myridenavigation.tabBarItem.image = [UIImage imageNamed:#"cool.png"];
[myridenavigation setTitle:#"Login"];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:myridenavigation];
viewsArray = [NSArray arrayWithObjects:nav0,nav1,nav2,nav3,nav4,nav5,nav6,nav7,nav8, nil];
tabbarController = [[UITabBarController alloc] init];
[tabbarController setViewControllers:viewsArray];
self.window.rootViewController = tabbarController;
Now i am getting data from the URL and want to assign it as dynamically navigation item. But i m puzzeled now, any idea how to do it.
NSString *loginstring = [NSString stringWithFormat:#"%#nvgationarray.php",mydomainurl];
NSMutableData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: loginstring]];
NSDictionary *allData = [NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil];
int i = 0;
for(NSDictionary *stat in allData)
{
NSString *ssprst = [stat objectForKey:#"tab_type"];
NSString *ssprst1 = [stat objectForKey:#"tab_name"];
NSString *ssprst2 = [stat objectForKey:#"tab_id"];
NSString *ssprst3 = [stat objectForKey:#"icon"];
NSLog(#"all data ===== :::: %# %# %# %#",ssprst,ssprst1,ssprst2,ssprst3);
NSLog(#"++++++++++++++++++++++++++++++++++++++++++++++");
AbcViewController *myridenavigation = [[AbcViewController alloc] init];
myridenavigation.tabBarItem.image = [UIImage imageNamed:#"cool.png"];
[myridenavigation setTitle:#"Login"];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:myridenavigation];
i++;
}
if you want to retain memory UINavigationController . I suggest you hold it in NSMutableArray.
In header file
#property(nonatomic, strong) NSMutableArray* arrayNavigationCont;
In implementation file
#synthesize arrayNavigationCont = _arrayNavigationCont;
do not forget also to init array in somewhere relevant like viewDidLoad
self.arrayNavigationCont = [[NSMutableArray alloc] init];
In your method
...
for(NSDictionary *stat in allData)
{
...
AbcViewController *myridenavigation = [[AbcViewController alloc] init];
myridenavigation.tabBarItem.image = [UIImage imageNamed:#"cool.png"];
[myridenavigation setTitle:#"Login"];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:myridenavigation];
[self.arrayNavigationCont addObject:nav1]
i++;
}
Later you can access you navigationConts somewhere again
for(int i = 0; i < [self.arrayNavigationCont count]; i++)
{
UINavigationController *nav1 = [self.arrayNavigationCont objectAtIndex:i];
//do sth with nav1 to your like
}
In my implementation of TTLauncherView, only loads the first page. Why?
I have 47 items in array, 47 items div 9 items by page, I should have 6 pages.
Thanks for helping.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSMutableString *jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *results = [jsonString JSONValue];
NSArray *photos = [[results objectForKey:#"photosets"] objectForKey:#"photoset"];
launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
launcherView.backgroundColor = [UIColor whiteColor];
launcherView.delegate = self;
launcherView.columnCount = 3;
launcherView.persistenceMode = TTLauncherPersistenceModeNone;
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
for (NSDictionary *photo in photos)
{
NSString *iconURLString = [NSString stringWithFormat:#"http://farm%#.static.flickr.com/%#/%#_%#_s.jpg",
[photo objectForKey:#"farm"], [photo objectForKey:#"server"], [photo objectForKey:#"primary"], [photo objectForKey:#"secret"]];
NSDictionary *title = [photo objectForKey:#"title"];
NSString *itemTitle = [title objectForKey:#"_content"];
TTLauncherItem *itemMenu = [[[TTLauncherItem alloc] initWithTitle:itemTitle
image:iconURLString
URL:nil
canDelete:NO] autorelease];
[itemArray addObject:itemMenu];
}
launcherView.pages = [NSArray arrayWithObject: itemArray];
[self.view addSubview:launcherView];
}
As I recall, TTLauncherView doesn't break up the TTLauncherItem's into pages automatically. You need an array of arrays. All of the launcher item's in the first array will be on the first page, all the launcher item's in the second array will be on the second page etc. It has been a long time since I've used it, but I think that's how it worked.
My modified code with the hint of #Darren
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSMutableString *jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *results = [jsonString JSONValue];
NSArray *photos = [[results objectForKey:#"photosets"] objectForKey:#"photoset"];
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
NSMutableArray *pageArray = [[NSMutableArray alloc] init];
NSNumber *countPage = [[NSNumber alloc] initWithInt:0];
for (NSDictionary *photo in photos)
{
NSString *iconURLString = [NSString stringWithFormat:#"http://farm%#.static.flickr.com/%#/%#_%#_s.jpg",
[photo objectForKey:#"farm"], [photo objectForKey:#"server"], [photo objectForKey:#"primary"], [photo objectForKey:#"secret"]];
NSString *photoCount = [photo objectForKey:#"photos"];
NSDictionary *title = [photo objectForKey:#"title"];
NSString *itemTitle = [title objectForKey:#"_content"];
TTLauncherItem *itemMenu = [[[TTLauncherItem alloc] initWithTitle:itemTitle
image:iconURLString
URL:nil
canDelete:NO] autorelease];
itemMenu.badgeValue = photoCount;
[itemArray addObject:itemMenu];
int value = [countPage intValue];
countPage = [NSNumber numberWithInt:value + 1];
if (countPage == [NSNumber numberWithInt:9]){
countPage = [NSNumber numberWithInt:0];
[pageArray addObject:itemArray];
itemArray = [[NSMutableArray alloc] init];
}
}
[pageArray addObject:itemArray];
launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
launcherView.backgroundColor = [UIColor blackColor];
launcherView.delegate = self;
launcherView.columnCount = 3;
launcherView.persistenceMode = TTLauncherPersistenceModeNone;
launcherView.pages = pageArray;
[self.view addSubview:launcherView];
}
I have tried using a couple of different search bar examples but I have not had any luck with adding a search bar. I would really appreciate any help that can be provided. The following is the code and I was using the search bar controller example.
if(CurrentLevel == 0) {
//Initialize our table data source
NSArray *tempDict = [[NSArray alloc] init];
self.tableDataSource = tempDict;
[tempDict release];
Midwest_DigestiveAppDelegate *AppDelegate = (Midwest_DigestiveAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource = [AppDelegate.data valueForKey:#"Rows"];
//Initialize the array.
NSMutableArray *listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObjectsFromArray:tableDataSource];
//Initialize the copy array.
NSMutableArray *copyListOfItems = [[NSMutableArray alloc] init];
//Add the search bar
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
}
else
self.navigationItem.title = CurrentTitle;
//Initialize the array.
NSMutableArray *listOfItems = [[NSMutableArray alloc] initW];
[listOfItems addObjectsFromArray:tableDataSource];
//Initialize the copy array.
NSMutableArray *copyListOfItems = [[NSMutableArray alloc] init];
//Add the search bar
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
}
- (void) searchTableView {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in listOfItems)
{
NSArray *array = [dictionary objectForKey:#"Title"];
[searchArray addObjectsFromArray:array];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
[searchArray release];
searchArray = nil;
}
I am getting this error that I can't figure out.
error: Automatic Reference Counting Issue: Receiver type 'pageAppViewController' for instance message does not declare a method with selector 'createContentPages'
I posted my code below. I do have a method called createContentPages in my class pageAppViewController. What does this mean and what is causing it?
// contentViewController.m
#import "contentViewController.h"
#import "pageAppViewController.h"
#implementation contentViewController
#synthesize theImageView, dataObject;
#synthesize image;
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//error occurs on this next line
pageAppViewController *newContent = [[pageAppViewController alloc] init];
self.image = [newContent createContentPages];
[self.theImageView setImage: ((pageAppViewController *) [self.image objectAtIndex:0]) .images];
}
error: Automatic Reference Counting Issue: Receiver type 'pageAppViewController' for instance message does not declare a method with selector 'createContentPages'
//
// pageAppViewController.m
#import "pageAppViewController.h"
#implementation pageAppViewController
#synthesize pageController;
#synthesize bookContent;
#synthesize images;
- (NSArray *) createContentPages
{
UIImage *zero = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"TitlePage.png"]];
UIImage *one = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"Page1.png"]];
UIImage *two = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"Page2.png"]];
UIImage *three = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"Page3.png"]];
UIImage *four = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"Page4.png"]];
UIImage *five = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"Page5.png"]];
NSMutableArray *pageContent = [[NSMutableArray alloc] init];
for (int i = 1; i < 7; i++)
{
pageAppViewController *content = [[pageAppViewController alloc] init];
if (i == 1)
{
content.images = zero;
[pageContent addObject:content];
}
else if (i == 2)
{
content.images = one;
[pageContent addObject:content];
}
else if (i == 3)
{
content.images = two;
[pageContent addObject:content];
}
else if (i == 4)
{
content.images = three;
[pageContent addObject:content];
}
else if (i == 5)
{
content.images = four;
[pageContent addObject:content];
}
else if (i == 6)
{
content.images = five;
[pageContent addObject:content];
}
}
bookContent = [[NSArray alloc] initWithArray: pageContent];
return bookContent;
}
have you the
- (NSArray *) createContentPages;
Method declaration in the .h File of the Controller?
i am parsing a json object and storing the song objects in songs, a nsmutable array.
while am displaying the image of the song i.e. while accessing the object from the array its giving all values nil in that object.
in the following code in setSongsScrollView method, in for loop while accessing the song object from songs array its showing nill in the debugger and crashing with error EXEBadacess.But the count of that array is giving correct.
can any body help me out please
- (void)viewWillAppear:(BOOL)animated{
[super viewDidLoad];
[self parsingTheStation];
[self load_images];
[self setSongsScrollView];
}
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
- (void)parsingTheStation{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http:...."]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *jsonString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:nil];
songs =[[NSMutableArray alloc]init];
NSArray *songObjects = [dictionary objectForKey:#"songs"];
for(NSDictionary *s in songObjects){
aSong = [[Song alloc] init];
aSong.artist = [s objectForKey:#"by"];
aSong.genre = [s objectForKey:#"genre"];
aSong.cover = [s objectForKey:#"cover"];
aSong.song_id = [s objectForKey:#"id"];
aSong.rank = [s objectForKey:#"rank"];
aSong.title = [s objectForKey:#"title"];
aSong.link = [s objectForKey:#"link"];
[songs addObject:aSong];
[aSong release];
}
NSLog(#"total number of songs is : %d",[songs count]);
}
-(void)setSongsScrollView {
songsContainer = [[UIScrollView alloc]init];
int songsCount = [self.songs count];
//totla no. of songs we get +4
int tSongs = songsCount+4;
int n = sqrt(tSongs);
int p = n,q = n;
int remSongs = tSongs-(n*n);
if(remSongs >= n){
q = q+(remSongs/n);
if((remSongs%n)>0)
q++;
}else q++;
for(int i=0;q>p;i++){
q--;
p++;
}
NSLog(#"total songs..%d",tSongs);
NSLog(#"total rows..%d",q);
NSLog(#"total columns..%d",p);
songsContainer.contentSize = CGSizeMake(120*q, 120*p);
int x =0, y=240, col=1;
for(int i=0;i<songsCount;i++){
CGRect imgFrame = CGRectMake(x, y, 118, 118);
NSLog(#"songs conunt ...%d",[songs count]);
Song *thesong = [[Song alloc]init];
thesong = [self.songs objectAtIndex:i];
NSString *filename = [NSString stringWithFormat:#"%#/%#", [LazyImageView dataPath], [thesong.cover lastPathComponent]];
UIImageView *tempImg = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filename]];
tempImg.tag = i;
tempImg.frame = imgFrame;
[songsContainer addSubview:tempImg];
[tempImg release];
[thesong release];
y += 120;
if(y>=(120*p)){
NSLog(#"total y..%d",y);
col++;
x += 120;
if(col>=3)
y=0;
else
y=240;
}
}
NSLog(#"total y..%d",y);
NSLog(#"content size..%d,%d",120*q,120*p);
}
-(void)load_images{
for(int i=0;i<[songs count];i++){
Song *rsong = [[Song alloc]init];
rsong = [self.songs objectAtIndex:i];
lazyBigImg = [[LazyImageView alloc] init];
NSURL* url = [NSURL URLWithString:rsong.cover];
[lazyBigImg loadImageFromURL:url];
[lazyBigImg release];
[rsong release];
}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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 {
[songs release];
[lazyBigImg release];
[onAirBtn release];
[chartsBtn release];
[dealsBtn release];
[searchBtn release];
[stNameLbl release];
[aSong release];
[songsContainer release];
[super dealloc];
}
#end
Marcel has basically got the right answer but I think a little more explanation is needed. Look at the following lines from setSongsScrollView:
Song *thesong = [[Song alloc]init];
The above line allocates a new Song that you own and assigns a reference to it to thesong
thesong = [self.songs objectAtIndex:i];
The above line replaces that reference with a new reference to a song from the array that you don't own. Remember that: you do not own the song referenced by thesong now. There are now no more references left to the object you just allocated, but you still own it. The object has therefore leaked.
NSString *filename = [NSString stringWithFormat:#"%#/%#", [LazyImageView dataPath], [thesong.cover lastPathComponent]];
Use stringByAppendingPathComponent: to build file paths, not stringWithFormat:.
UIImageView *tempImg = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filename]];
tempImg.tag = i;
tempImg.frame = imgFrame;
[songsContainer addSubview:tempImg];
[tempImg release];
[thesong release];
The last line in the above sequence releases the object referenced by thesong. As noted above, you do not own that object. You must not release it, but you have anyway. This means that, at some point, may be now, maybe later, the object will be deallocated while something (probably the array) still thinks it has a valid reference. That's what causes the crash.
-(void)load_images{
for(int i=0;i<[songs count];i++){
Song *rsong = [[Song alloc]init];
rsong = [self.songs objectAtIndex:i];
lazyBigImg = [[LazyImageView alloc] init];
NSURL* url = [NSURL URLWithString:rsong.cover];
[lazyBigImg loadImageFromURL:url];
[lazyBigImg release];
[rsong release];
}
}
The above method contains exactly the same error.
You're creating a new Song instance (thesong), then assign this very instance to a song presumably already in the array. That makes no sense at all and is probably responsible for the memory error.
You shouldn't need to be creating new Songs if they are already in the array. Instead:
Song *thesong = [self.songs objectAtIndex:i];
Also look into using the Objective-C 2.0 for-each loop syntax.