这是另一个“我确信这个问题有一个简单的答案”的问题,但我发现自己很困惑。
我正在使用模板中的拆分视图控制器。我成功地将 NSString 传递给 _detailItem 变量,但无法使用它来创建图像并将其加载到 UIImageView(名为“stripImage”)中。
它一直工作到: [self.stripImage setImage:[UIImage imageNamed: _detailItem]];
如果我将字符串文字放入同一行代码(如@“image20.jpg”),它工作正常。
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
NSLog(@"_detailItem is still: %@",_detailItem);
// correctly reports the name of the imagefile (for example: "image20.jpg"
[self.stripImage setImage:[UIImage imageNamed: _detailItem]];
NSLog(@"The image being shown is: %@",self.stripImage.image);
//returns "The image being shown is: (null)"
}
}
请帮助防止我的头爆炸。提前致谢。
...我已经尝试过这种方式:
(在我的主视图控制器中):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Selected row %i",[indexPath row]);
NSString *imageName = [[stories objectAtIndex:[indexPath row]] objectForKey:@"link"];
NSLog(@"The image is: %@", imageName);
// These two work, oddly enough...
self.detailViewController.detailTitle.text = [[stories objectAtIndex:[indexPath row]]
objectForKey:@"title"];
self.detailViewController.detailSubtitle.text = [[stories objectAtIndex:[indexPath row]]
objectForKey:@"subtitle"];
// this one, not so much...
[self.detailViewController loadUpImageWith:imageName];
}
在我的详细视图控制器中:
- (void)loadUpImageWith:(NSString *)what
{
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[NSString
stringWithFormat:@"strips/%@", what]];
NSLog(@"The file's url is: %@",path);
UIImage *img = [UIImage imageWithContentsOfFile:path];
NSLog(@"The resultant image is: %@",img);
stripImage.image = img;
}
但这是奇怪的事情......如果我用字符串文字替换变量(在这种情况下为“what”):
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[NSString
stringWithFormat:@"strips/%@", @"grumbles300.jpg"]];
...它的工作原理,显示一个图像。但我希望能够在那里使用变量!
帮助我欧比万克诺比!你是我唯一的希望。