【问题标题】:didSelectRowAtIndexPath gets called but not prepareForSeguedidSelectRowAtIndexPath 被调用但没有 prepareForSegue
【发布时间】:2016-06-27 00:18:41
【问题描述】:

这只发生在我第一次以全新方式运行应用程序时,即便如此,当我尝试重新创建以进行调试时,大部分时间它都能正常工作。

我在indexPath.row 选择了这首歌,一切都记录正确,除了 segue 没有被调用。

它调用didSelectRowAtIndexPath,但不调用prepareForSegue

一切都正确连接,因为如果我再试一次,它会完美运行,didSelectRowAtIndexPathprepareForSegue 都会被调用。 (我只是使用didSelectRowAtIndexPath 进行调试以记录我没有得到零数据。)

有没有人遇到过这个问题,或者对如何调试有任何想法?谢谢!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        MPMediaItem *selectedItem = [[albumsArrayForTVC objectAtIndex:indexPath.section] representativeItem];
        NSString *albumDetailTitle = [selectedItem valueForProperty:MPMediaItemPropertyAlbumTitle];
        MPMediaQuery *tempAlbumMPMediaQuery = [MPMediaQuery songsQuery];
        MPMediaPropertyPredicate *albumPredicate = [MPMediaPropertyPredicate predicateWithValue: albumDetailTitle forProperty: MPMediaItemPropertyAlbumTitle];
        [tempAlbumMPMediaQuery addFilterPredicate:albumPredicate];
        NSArray *albumTracksArray = [tempAlbumMPMediaQuery items];
        MPMediaItem *rowItemSong = [[albumTracksArray objectAtIndex:indexPath.row] representativeItem];
        NSLog(@"track: %@", rowItemSong);
        NSString *song = [rowItemSong valueForProperty:MPMediaItemPropertyTitle];
        NSLog(@"track name: %@", song);
}

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

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

        // send to now playing
        NSUInteger selectedSection = [[self.tableView indexPathForSelectedRow] section];
        NSUInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];        
        NSArray *albumTracksArray = [self albumTracksForSegue:[[albumsArrayForTVC objectAtIndex:selectedSection] representativeItem]];
        MPMediaItem *rowItemSong = [[albumTracksArray objectAtIndex:selectedIndex] representativeItem];
        MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];
        [musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:albumTracksArray]];
        [musicPlayer setNowPlayingItem:rowItemSong];
        [musicPlayer play];
        NSLog(@"Row Item Song: %@", rowItemSong);
    }
}

这很奇怪,因为它不是我每次都能重新创建的东西,这就是为什么我很难弄清楚这里发生了什么,无论是系统音乐播放器的东西还是我的东西代码等

【问题讨论】:

    标签: ios objective-c uitableview audio-player mpmediaitem


    【解决方案1】:

    首先,切勿使用UITableViewCell 链接您的转场。当您想从 UITableViewController 转到另一个控制器时,请使用控制器链接 segue。按住 Control 键,将控制器顶部的黄色圆圈拖到您要转入的控制器上,并为其指定一些标识符。

    其次,在您的 didSelectRowAtIndexPath 方法中,使用您给定的标识符调用函数 performSegueWithIdentifire 以连接到另一个控制器。

    希望这会有所帮助。 :)

    【讨论】:

    • 那么你会使用UITableViewCell链接你的segue吗?只有当有一个转场/标识符时才会这样吗?然后我应该同时使用prepareForSegue:didSelectRowAtIndexPath,还是应该只使用其中一个?如果我使用prepareForSegue:,我是否应该不使用`if([segue.identifier isEqualToString:@"nowPlaying"]){`,而是将那部分放在didSelectRowAtIndexPath中,还是应该在两者中都有?跨度>
    • 使用UITableViewCell 链接你的segue 是一种不好的做法。你必须使用didSelectRowAtIndexPath 来调用segue。在您的prepareForSegue 中,您可以使用if(segue.identifier == "your identifier") 检查segue 标识符并进行相应处理。
    【解决方案2】:

    在您的 didSelectRowAtIndexPath: 方法中调用 [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:nil];。请注意,didSelectRowAtIndexPath:prepareForSegue: 都是 UIViewController 的方法,而不是 UITableViewCell 的方法。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
        //If you have only one segue then do just this else add your if condition for different segues 
        [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:nil]; 
    }
    

    【讨论】:

    • 我应该同时使用prepareForSegue:didSelectRowAtIndexPath,还是应该只使用其中之一?如果我仍在使用prepareForSegue:,是否还需要指定` if([segue.identifier isEqualToString:@"nowPlaying"]){`?
    • - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //If you have only one segue then do just this else add your if condition for different segues [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:nil]; }
    • 我现在看到 didSelectRowAtIndexPath:。那么不要使用prepareForSegue: 吗?
    • @SRMR 您需要在didSelectRowAtIndexPath 方法中调用prepareForSegue:。只需在您的代码中复制上述注释即可。在我的答案中添加,只需复制并输入您的代码即可。
    • 啊,好吧,现在说得通了。如果我只有一个 segue,那么根本不使用 didSelectRowAtIndexPathprepareForSegue 是否有意义?
    猜你喜欢
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 2023-04-01
    • 2015-05-17
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多