I know this is simple, but iv tried and couldnt fix it. what am i missing?

I have an array of images placed in table view cells. once an image is clicked segue to new viewcontroller. the new view controller would then play the video that corresponds with the image.

I want to pass the selected image the next view and play the the right video. how do I pass vid1 or vid2 or vid3 to the next controller?

- (void)viewDidLoad
{
[super viewDidLoad];
Videos *vid1 = [Videos new];
vid1.name = @"name1";
vid1.detail = @"18:39 - 19:10";
vid1.imageFile = @"img1.png";
vid1.url = @"http://pv...mp4";

Videos *vid2 = [Videos new];
vid2.name = @"name2";
vid2.detail = @"6:51 - 7:10";
....
videos = [NSArray arrayWithObjects:vid1, vid2, vid3, nil];
}

// TABLE VIEW //
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
static NSString *CellIdentifier = @"VideoCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Videos * video = [videos objectAtIndex:indexPath.row];

UIImageView *videoImageView = (UIImageView *)[cell viewWithTag:100];
videoImageView.image = [UIImage imageNamed:video.imageFile];

return cell;
}

// SEGUE // - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"showVideo"]) {

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:
                              [[sender superview] tag]];

    playVideoViewController *destViewController = segue.destinationViewController;
    // problem here
    destViewController.video = [videos objectAtIndex:(long)indexPath.row];

    NSLog(@"Segue to video page");
}
}

In my playVideoViewController:

-(void)showvideo {
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];

[center addObserver:self selector:@selector(mPMoviePlayerLoadStateDidChangeNotification) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[center addObserver:self selector:@selector(readytoplay) name:MPMoviePlayerReadyForDisplayDidChangeNotification object:nil];

NSURL* url = [NSURL URLWithString:
         @"http://pv...mp4"];

//NSLog(@"url: %@", url);

self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
}

How do I retrieve the correct the item from the array and play it?

Thanks for looking

有帮助吗?

解决方案

I haven't used storyboard almost for two years and am not familiar with it. But some part of your code doesn't make sense.

In prepareForSegue:sender

if ([[segue identifier] isEqualToString:@"showVideo"]) 
{
    // Here I don't think you were getting a right index path
    NSIndexPath *selectedRowIndexPath = [self.tableview indexPathForSelectedRow];

    playVideoViewController *destViewController = segue.destinationViewController;

    // so maybe you are always getting the first video in the array or one you don't expect  
    destViewController.video = [videos objectAtIndex:(long)selectedRowIndexPath.row];
}

In playVideoViewController

- (void)showVideo
{
    // I got this part from apple's doc and some other SO
    // But the idea I want to point out is you should get url from self.video which you assigned on prepareForSegue:sender in the previous controller.
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:self.video.url]];
    player.movieSourceType = MPMovieSourceTypeFile;
    player.controlStyle = MPMovieControlStyleDefault;
    [player play];
    [self.view addSubview: player.view];
    [player setFullscreen:YES animated:YES];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top