Iphone 如何为多个对象准备ForSegue:UIImageView和audiofile
如何在prepareforSegue中为destinationViewController添加音频文件以及UIImageviewIphone 如何为多个对象准备ForSegue:UIImageView和audiofile,iphone,uicollectionview,uistoryboardsegue,Iphone,Uicollectionview,Uistoryboardsegue,如何在prepareforSegue中为destinationViewController添加音频文件以及UIImageview - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"showDetail"]) { NSIndexPath *selectedIndexPath = [[self.collect
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"])
{
NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];
// load the image, to prevent it from being cached we use 'initWithContentsOfFile'
NSString *imageNameToLoad = [NSString stringWithFormat:@"%d_full", selectedIndexPath.row];
NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"jpg"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.image = image;
}
}
谢谢我真的不明白你的意思。您已将UIImage实例传递给detailViewController。如果你想传递其他物体,这是类似的。谢谢,我试过了,但在每一个细胞中,它都在播放它们1。无论选择哪个单元格。而我希望它应该为第一个单元格播放Theme1,为第二个单元格播放Theme2,这是因为您的文件路径正在获取第一个元素
[audioArray objectAtIndex:0]
。尝试使用selectedIndexPath.row,就像对图像所做的那样。我将其更改为NSString*filePath=[audioArray objectAtIndex:[indexPath row]];但是现在我必须把它分配给像cell.image.image=[UIImage ImageName:imageToLoad]这样的单元格;如何将audioplayer分配给单元格表视图单元格的子类,并添加属性来存储其音频文件路径。在didSelectRowAtIndexPath中,分配音频播放器。
audioArray =[[NSArray alloc] initWithObjects:@"Theme1", @"Theme2", @"Theme3", @"Theme4", nil];
NSString *filePath = [audioArray objectAtIndex:0];
NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: audioFilePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];