从标签栏控制器打开带有后退按钮的单独 ViewController

Opening a Separate ViewController with back button from a tabbar controller

我有一个 Tabbar 控制器,在那个控制器中我显示了超过 5 个 viewcontroller。一切都按预期工作。

但现在我有一个 ViewController,它有 UiTableView,在单元格上单击我想打开一个 DetailView 控制器。

细节ViewController 将在导航视图控制器中有一个标题和返回列表的后退按钮。

请帮我用Swift怎么做?

你想要显示 detailVC 的 vc 应该嵌入到 navigationController 中,然后使用 push/pop 来管理 show/hide

let vc = ///

self.navigationController?.pushViewController(vc, animated: true)

//

self.navigationController?.popViewController(animated: true)
import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func moveToNexScreen(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let nextVc = storyboard.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
    let navigationVc = UINavigationController(rootViewController: nextVc)
    present(navigationVc, animated: false, completion: nil)
}
}

class NextViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(self.closeBackButtonPressed))        
}

@objc func closeBackButtonPressed(){
    self.dismiss(animated: false, completion: nil)
}
}

将您的 detailView 放入 UINavigationController(rootViewController: detailVc) 中,然后您可以显示它,并且要有一个后退按钮,您可以通过编程方式在 detailsView 上添加一个 barbutton 项目

使用 -

#import <DetailViewController.h>

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

    NSIndexPath *indexPath = [_tableView indexPathForCell:sender];

    DetailViewController *details = (DetailViewController *)segue.destinationViewController;

    details.dictionary = _json[indexPath.row]; // to pass data

}

在故事板中 -

Select Cell and drag a segue to Details view controller

也许这个故事板插图会对您有所帮助。显示你需要的vcs就干净了。