Reputation: 3107
I am trying to pass the value of a string from an array to an instance of ImageViewer
when building a table view cell, but I am getting nil values. Here is the code:
NSString *file = [[NSString alloc] init ];
file = [photoFiles objectAtIndex:indexPath.row];
ImageViewer *imageView = [[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:nil];
imageView.fileName = file;
[self.navigationController pushViewController:imageView animated:YES];
[imageView release];
Can you help me please to fix this problem?
Upvotes: 0
Views: 92
Reputation: 3666
I have my way to do That ... may be this will help you ...
I am having NSarray in my table view ... and the next view I have NsDictionary
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int actualRowIndex = [[filteredIndexs objectAtIndex:indexPath.row] intValue];
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
selectedMeal *selctedMealView = [[selectedMeal alloc] initWithNibName:@"selectedMeal" bundle:nil];
selctedMealView.offerDetails = [offers objectAtIndex:actualRowIndex];
[self.navigationController pushViewController:selctedMealView animated:YES];
//[selctedMealView changeText:[arryList objectAtIndex:indexPath.row]];
[selctedMealView release];
}
in here selectedmeal is my next View and offers is my NSArray in table View also the offerDetails is my NSDictionary in selected meal view
also in table View I created NSdictionary and settheobject like this
NSString *o_resturant_name = [offer objectForKey:@"restaurant_name"];
[[Globals sharedGlobals].resturantList addObject:o_resturant_name];
and the selectedmeal view in viewdidload I read that value ...
NSString *resturantNameText = [offerDetails objectForKey:@"restaurant_name"];
This is working perfectly for me ...
may be this will help you
Upvotes: 0
Reputation: 5611
If imageView.fileName = file
is setting a nil
value, you probably should consider analyzing the contents of the photoFiles
array.
It could be possible that this array has no values at the indexPath.row
index. You should check this through the debugger, or with a log print:
NSLog(@"Array contents: %@", [photoFiles description]);
You could write this code in a more concise way. I.e.:
ImageViewer *imageView = [[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:nil];
imageView.fileName = [photoFiles objectAtIndex:indexPath.row];
[self.navigationController pushViewController:imageView animated:YES];
[imageView release];
Also, you should avoid pointing to a nil
NSBundle
. I.e. Change the first row to:
ImageViewer *imageView = [[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:[NSBundle mainBundle]];
But it should work also this way, since the class name and the Nib name are the same:
ImageViewer *imageView = [[ImageViewer alloc] init];
Try it and let me know if something changed.
Upvotes: 0
Reputation: 1751
check the line
file = [photoFiles objectAtIndex:indexPath.row];
and
Check the properties for filename in imageviewer
Upvotes: 1
Reputation: 763
Well, there's not enough information here to give an answer (you'll need to step through it with a debugger and figure out why you're getting a nil value back), and you have at least one memory leak. Here's how I'd rewrite the code you present:
ImageViewer *imageView = [[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:nil];
imageView.fileName = [photoFiles objectAtIndex:indexPath.row];
[self.navigationController pushViewController:imageView animated:YES];
[imageView release];
Upvotes: 0
Reputation: 14123
Firstly, you don't have to allocate memory to NSString. Secondly, have you set properties for fileName in ImageViewer?
Upvotes: 0