我有一个表格视图,当选择一行时,它会带您进入详细页面。详细信息页面上有图像视图。我正在尝试在选择行时从表视图设置图像视图的图像,但是当选择行并且详细视图出现时,除了白页之外什么都没有。
像这样:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
GRSBandDetailViewController *detail = [[GRSBandDetailViewController alloc]initWithNibName:@"GRSBandDetailViewController" bundle:nil];
if (indexPath.row == 0)
{
[detail.bandImage setImage:[UIImage imageNamed:@"AbovetheUnderground.png"]];
[self.navigationController pushViewController:detail animated:YES];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
这是GRSBandDetailViewController的.h文件:
@interface GRSBandDetailViewController : UIViewController
{
IBOutlet UIImageView *bandImage;
}
@property (nonatomic, strong) UIImageView *bandImage;
@end
答案 0 :(得分:4)
这种情况正在发生,因为在第一次调用-[UIViewController view]
期间,视图被懒惰地加载。因此,当您尝试设置图像时,detail.bandImage
为零。
在UIImage
初始值设定项中或通过属性提供GRSBandDetailViewController
。然后将bandImage.image
设置为viewDidLoad
中此属性的值。
答案 1 :(得分:3)
我通过将此问题添加到详细视图控制器来修复此问题。
@property (nonatomic, strong) UIImage *passedImage;
@property (nonatomic, strong) NSString *titleString;
viewDidLoad现在有这个:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = titleString;
bandImage.image = passedImage;
}
表视图中的didSelectRowAtIndexPath方法如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
GRSBandDetailViewController *detail = [[GRSBandDetailViewController alloc]initWithNibName:@"GRSBandDetailViewController" bundle:nil];
if (indexPath.row == 0)
{
[self.navigationController pushViewController:detail animated:YES];
detail.passedImage = [UIImage imageNamed:@"AbovetheUnderground.png"];
detail.titleString = @"Above the Underground";
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}