我知道这很简单,但我试过并且无法修复它。我错过了什么?
我在表格视图单元格中放置了一系列图像。一旦图像被点击segue到新的viewcontroller。然后,新的视图控制器将播放与图像对应的视频。
我想将所选图像传递到下一个视图并播放正确的视频。如何将vid1或vid2或vid3传递给下一个控制器?
- (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");
}
}
在我的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;
}
如何从数组中检索正确的项目并进行播放?
感谢您寻找
答案 0 :(得分:0)
我几乎没有使用故事板两年而且我不熟悉它。但是你的代码的某些部分没有意义。
在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];
}
在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];
}