【发布时间】:2023-04-06 17:46:02
【问题描述】:
我是 iOS 开发新手,目前正在学习视频教程。我在试验一个非常简单的应用时遇到了问题。
问题在于在拆分视图控件 iPad 应用程序中将图片加载到 UIImage。该应用程序基于 XCode 主从应用程序模板。我创建了一个包含 NSObject 变量和两个属性的数组。
@interface Burger : NSObject
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *filename;
@end
- (void)viewDidLoad {
[super viewDidLoad];
photos = [[NSMutableArray alloc]init];
Burger *pic = [[Burger alloc]init];
pic.name = @"Bacon";
pic.filename = @"burger-bacon";
[photos addObject:pic];
pic = [[Burger alloc]init];
pic.name = @"Cheese";
pic.filename = @"burger-cheddar";
[photos addObject:pic];
pic = [[Burger alloc]init];
pic.name = @"Hawaii";
pic.filename = @"burger-hawaii";
[photos addObject:pic];
pic = [[Burger alloc]init];
pic.name = @"Koko z jajkiej kurzym";
pic.filename = @"burger-jajo";
[photos addObject:pic];
pic = [[Burger alloc]init];
pic.name = @"Kozi ser";
pic.filename = @"burger-kozi";
[photos addObject:pic];
pic = [[Burger alloc]init];
pic.name = @"Łosoś";
pic.filename = @"burger-losos";
[photos addObject:pic];
pic = [[Burger alloc]init];
pic.name = @"Vege (Soczewica)";
pic.filename = @"burger-soczewica";
[photos addObject:pic];
}
我希望嵌套在DetailView 中的UImageView 显示在MasterView 控制器中选择的对象的图片。我得到它来显示MasterView表中的图片
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Burger *current = [photos objectAtIndex:indexPath.row];
UIImage *image = [UIImage imageNamed:[current filename]];
[cell.imageView setImage:image];
self.title = self.currentPhoto.name;
cell.textLabel.text = [current name];
cell.detailTextLabel.text = [current price];
return cell;
}
当我指向一个特定的文件名时它也可以工作:
- (void)configureView {
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem notes];
UIImage *image = [UIImage imageNamed: @"burger-bacon"];
[self.currentImage setImage:image];
self.title = self.currentPhoto.name;
}
}
但是当我尝试像这样显示所选元素的图片时:
- (void)configureView {
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem notes];
UIImage *image = [UIImage imageNamed: self.currentPhoto.filename];
[self.currentImage setImage:image];
self.title = self.currentPhoto.name;
}
}
我在调试区得到了这几行:
2015-06-23 12:13:25.964 SlowFoodiPad[22177:744765] CUICatalog: Invalid asset name supplied: (null)
这意味着 segues 工作并且照片文件似乎很好。指向要显示的正确文件似乎存在问题。任何想法如何解决它?
【问题讨论】:
-
stackoverflow.com/questions/22011106/… 请试试这个。我有同样的问题,这为我解决了。我还为文件名添加了扩展名。
-
我认为问题出在 self.currentPhoto 对象中。您是否为 currentPhoto 对象赋值?检查它是否为零。
标签: ios objective-c uisplitviewcontroller