【问题标题】:Load photos to DetailViewController from NSMutableArray从 NSMutableArray 将照片加载到 DetailViewController
【发布时间】: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


【解决方案1】:

好的,我设法部分解决了问题。我编辑了 MasterViewController 的 prepareForSegue 方法:

DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
    Burger *c = photos [indexPath.row];
    [controller setDetailItem:c];
    //this line helped
    [controller setCurrentPhoto:c];

现在显示适当的照片,尽管调试区域不断向我显示此消息:

CUICatalog: Invalid asset name supplied: (null)

每次我点击 MasterView 表中的元素时都会出现两条相同的行。

【讨论】:

    【解决方案2】:

    目前还不清楚何时调用“-(void)configureView”。根据您的问题,我的猜测是它实际上是在 prepareForSegue 之前调用的。或者至少,第一次,它是在你真正拥有图像之前被调用的。您正在检查“if (self.detailItem)”,但没有检查 if (self.currentPhoto)。

    所以可能是第一次调用configure view,没有self.currentPhoto所以不能设置。然后当 segue 实际准备好时(prepareForSegue :) 你设置当前照片,所以当再次调用配置视图时,它就在那里。

    如果您粘贴更多显示触发 segue 的视图控制器代码(可能只是故事板,但从上面的内容不清楚)以及 configureView 时,那么我们肯定会知道我的猜测是否正确。

    【讨论】:

      【解决方案3】:

      好的,有一些进展。现在我只收到两行此消息,并且仅在应用程序的初始启动时:

      CUICatalog: Invalid asset name supplied: (null)    
      

      我在这里注释掉了几行。您能看到其他导致问题的原因吗?

      - (void)setDetailItem:(id)newDetailItem {
      if (_detailItem != newDetailItem) {
          _detailItem = newDetailItem;
      // Update the view.
          [self configureView];
      }
      }
      
      - (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];
      } 
      //}
      
      - (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
      UIImage *image = [UIImage imageNamed:self.currentPhoto.filename];
      [self.currentImage setImage:image];
      self.title = self.currentPhoto.name;
      self.detailDescriptionLabel.text = [self.detailItem notes];
      //    [self configureView];
      }
      

      这是 prepareForSegue 方法:

      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      if ([[segue identifier] isEqualToString:@"showDetail"]) {
      
         DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
          NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
          Burger *c = photos [indexPath.row];
          [controller setDetailItem:c];
      
          //adding this line helped
          [controller setCurrentPhoto:c];
      
          controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
          controller.navigationItem.leftItemsSupplementBackButton = YES;
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多