我正在尝试在构建表格视图单元格时将字符串的值从数组传递给ImageViewer
的实例,但我得到的是nil值。这是代码:
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];
你可以帮我解决这个问题吗?
答案 0 :(得分:1)
检查线
file = [photoFiles objectAtIndex:indexPath.row];
和
检查 imageviewer
中文件名的属性答案 1 :(得分:0)
首先,您不必将内存分配给 NSString 。其次,您是否在 ImageViewer 中设置 fileName 的属性?
答案 2 :(得分:0)
如果imageView.fileName = file
设置了nil
值,您可能应该考虑分析photoFiles
数组的内容。
此数组可能在indexPath.row
索引处没有值。您应该通过调试器或日志打印来检查:
NSLog(@"Array contents: %@", [photoFiles description]);
您可以更简洁地编写此代码。即:
ImageViewer *imageView = [[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:nil];
imageView.fileName = [photoFiles objectAtIndex:indexPath.row];
[self.navigationController pushViewController:imageView animated:YES];
[imageView release];
此外,您应该避免指向nil
NSBundle
。即将第一行更改为:
ImageViewer *imageView = [[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:[NSBundle mainBundle]];
但它也应该这样工作,因为类名和Nib名是相同的:
ImageViewer *imageView = [[ImageViewer alloc] init];
尝试一下,让我知道是否有变化。
答案 3 :(得分:0)
嗯,这里没有足够的信息来给出答案(你需要使用调试器逐步完成它并找出你为什么得到一个nil值),并且你至少有一次内存泄漏。这是我如何重写你提供的代码:
ImageViewer *imageView = [[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:nil];
imageView.fileName = [photoFiles objectAtIndex:indexPath.row];
[self.navigationController pushViewController:imageView animated:YES];
[imageView release];
答案 4 :(得分:0)
我有办法做到这一点......可能会帮助你...
我在我的桌面视图中有NSarray ...而下一个视图我有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];
}
在这里,selectedmeal是我的下一个View和offer是我的NSArray在表中查看offerDetails是我选择的膳食视图中的NSDictionary
也在表格中我创建了NSdictionary并像这样设置了对象
NSString *o_resturant_name = [offer objectForKey:@"restaurant_name"];
[[Globals sharedGlobals].resturantList addObject:o_resturant_name];
和viewdidload中的selectedmeal视图我读了那个值......
NSString *resturantNameText = [offerDetails objectForKey:@"restaurant_name"];
这对我来说非常合适......
可能会帮助你