简体   繁体   中英

Opening a Separate ViewController with back button from a tabbar controller

I have a Tabbar controller and in that controller I am showing more then 5 viewcontroller. everything is working as expected.

But now I have a ViewController that has UiTableView, On the cell click I want to open a DetailView Controller.

That DetailViewController will be Having a Title in the Navigation View Controller and the backbutton to go back again to list.

Please help me how to do it using Swift?

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)
}
}

Put your detailView inside UINavigationController(rootViewController: detailVc) and then you can present it and to have a back button you can add a barbutton item on the detailsView programatically

The vc you want to show the detailVC from should be embedded inside a navigationController , then use push/pop to manage show/hide

let vc = ///

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

//

self.navigationController?.popViewController(animated: true)

USE -

#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

}

In Storyboard -

Select Cell and drag a segue to Details view controller

Maybe this storyboard illustration will help you. It's clean to show the vcs you need. 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM