从NSMutableArray将照片加载到DetailViewController

时间:2015-06-23 10:35:25

标签: ios objective-c uisplitviewcontroller

我是iOS开发的新手,目前正在从视频教程中学习。我遇到了一个问题,我正在尝试一个非常简单的应用程序。

问题在于在分割视图控件iPad应用中将图片加载到 library(reshape2) df1 <- transform(df, result=as.character(result), red= factor(red, levels= unique(red))) dcast(df1, mult~red, value.var='result', fill='')[-1] # 1 0.9 0.8 0.7 #1 value1 #2 value2 #3 value3 #4 value4 。该应用程序基于XCode主 - 详细信息应用程序模板。我创建了一个包含UIImage变量和两个属性的数组。

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]; } 中的UImageView显示在DetailView控制器中选择的对象的图片。我让它在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;
    }    
}

这意味着segue工作,照片文件似乎没问题。指向要显示的正确文件似乎存在问题。任何想法如何解决它?

3 个答案:

答案 0 :(得分:0)

好的,我设法部分地解决了这个问题。我编辑了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表中的元素时,

两条相同的行。

答案 1 :(得分:0)

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

所以可能第一次调用configure视图时,没有self.currentPhoto,因此无法设置。然后当segue实际准备好(prepareForSegue :)时,你设置当前的照片,那么当再次调用configure视图时,它会在那里。

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

答案 2 :(得分:0)

好的,有一些进展。现在我只收到这条消息的两行,并且仅在应用程序的初始阶段:

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;
}
}