【发布时间】:2016-07-15 01:24:01
【问题描述】:
我有一个父视图控制器和一个子视图控制器,我需要将信息从子视图控制器中的按钮传递回父视图控制器,以便突出显示正确的 tableViewCell 文本。
在父视图控制器中,当点击单元格时,单元格文本会突出显示,子视图控制器会在屏幕上弹出并播放选定的歌曲。但是,在子视图控制器上有一个向后跳过和向前跳过按钮,因此当按下其中任何一个并再次显示父视图控制器时,单元格文本现在需要为不同的单元格突出显示而不是突出显示之前。
所以我已经为每个视图控制器添加了正确的委托样板代码,但我不确定从子视图控制器获取的最佳信息以发送回父视图控制器,以及如何使用它。
子视图控制器:
-(IBAction)indexChanged:(UISegmentedControl *)sender
{
NSURL *musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Blip_Select" ofType:@"wav"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
switch (self.segmentedControl.selectedSegmentIndex)
{
case 0:
[click play];
[click play];
[musicPlayer skipToPreviousItem];
break;
case 1:
[click play];
if ([musicPlayer playbackState] == MPMusicPlaybackStatePlaying) {
[musicPlayer pause];
} else {
[musicPlayer play];
}
break;
case 2:
[click play];
[click play];
[musicPlayer skipToNextItem];
default:
break;
}
}
我会在上述每个 switch 语句中添加一些内容,但 我不确定哪种方法最有效?
因为这是 父视图控制器中的样子:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AlbumsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
MPMediaItem *rowItemAlbum = [[albumsArrayForTVC objectAtIndex:indexPath.section] representativeItem];
NSString *albumDetailTitle = [rowItemAlbum valueForProperty:MPMediaItemPropertyAlbumTitle];
MPMediaQuery *albumMPMediaQuery = [MPMediaQuery albumsQuery];
MPMediaPropertyPredicate *albumPredicate = [MPMediaPropertyPredicate predicateWithValue: albumDetailTitle forProperty: MPMediaItemPropertyAlbumTitle];
[albumMPMediaQuery addFilterPredicate:albumPredicate];
NSArray *albumTracksArray = [albumMPMediaQuery items];
MPMediaItem *rowItemSong = [[albumTracksArray objectAtIndex:indexPath.row] representativeItem];
NSString *songTitle = [rowItemSong valueForProperty:MPMediaItemPropertyTitle];
cell.textLabel.text = songTitle;
cell.textLabel.highlightedTextColor = [UIColor brownColor];
return cell;
}
我知道我会添加某种方法,然后重新加载表格,但我不确定解决这个问题的最佳方法。有任何想法吗?谢谢!
【问题讨论】:
-
在您的子 VC 上实现委托,然后使用父 VC 上的数据源,然后重新加载表,例如设置一个 Int 来决定突出显示哪一行
-
尝试在子视图控制器中使用 Block 作为回调。并定义块在父视图控制器中的作用。
-
@Tj3n 这一切都说得通,也符合我的想法,但不确定如何执行您所说的 Int 部分,例如如何准确地实现+1/-1 在父 VC 中你知道吗?
-
@EricXuan 你在说什么区块?
标签: ios objective-c iphone uitableview mpmediaitem