ios - 从 UITableView(不是 UITableViewController)转到 UIViewController

标签 ios objective-c iphone uitableview

我在 UIViewController 中有一个 UITableView
当用户按下 UITableView 中的一个单元格时,我想显示另一个 UIViewController。所以我有 UIViewController“A”,里面有 UITableView“A”,我想单击 UITableView“A”中的一个单元格,然后推送到 UIViewController“B”。我尝试简单地将 UITableViewCell 的 segue 连接到 UIViewController 但这导致在按下单元格时没有任何反应。我也尝试了以下代码,但没有成功。同样,没有任何反应,没有错误。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    AlbumsViewController *albumsViewControllerObj = [mainStoryboard instantiateViewControllerWithIdentifier:@"AlbumsViewController"];
   [albumsViewControllerObj performSegueWithIdentifier:@"showAlbumDetail" sender:self];

}

到目前为止,我找到的所有建议都涉及从 TableView Controller 到另一个 View ,我知道该怎么做并且从未遇到过问题。出于某种原因,虽然我在从 TableView 移动到另一个 View Controller 时遇到了问题。如果重要(我认为可能),TableView“A”的委托(delegate)和数据源是 View Controller“A”。任何建议都很好,谢谢!

最佳答案

首先,将delegatedatasource设置为self。

其次,只需将 segue 从 ViewControllerA 拖到 ControllerB

第三,select时只执行segue

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  [self  performSegueWithIdentifier:@"showAlbumDetail" sender:nil];
    }

然后,在prepareForSegue中,将数据传递给destinnatinVC

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if (segue.identifier == yourid) {
        ViewControllerB *  = segue.destinationViewController;
        //Pass data
    }
}

关于ios - 从 UITableView(不是 UITableViewController)转到 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30204018/

相关文章:

iphone - UIWebView loadRequest 导致模拟器崩溃

ios - 其中 key :matchesQuery: is not giving any result

iphone - 按下按钮后立即退出键盘

iphone - 如何构建通用ios静态库

javascript - 如何在 React Native 中按日期分组

ios - 后台保存带来的小核心数据性能提升

ios - 在 Swift 3 和 Xcode 8 中使用加速度计和校准

ios - Objective-C 如何从与当前 View 重叠的选项卡栏中呈现操作表?

ios - 列出 Cocos2D 应用程序中所有事件节点/ Action /动画

ios - 对于运行 ios 12.1.2 的 iPhone XS Max,dyld_shared_cache_extract_dylibs 失败