Reusing UIViewController for two tab bar items

I have 1 tab bar controller in the storyboard and 1 associated UIViewController. I would like to reuse the same UIViewController to create a second item in the tab bar. When I create a second relation from the tab bar to view the controller, I need to specify 2 different element names. How can I reuse the same view controller and set different element names from the storyboard? If this cannot be done in the storyboard, then do I need to rename each of them in the controller class of the tab bar or is there a better way?

I was going to provide different data to view the controller in prepareforsegue.

UPDATE:

a bit more details and clarifications

enter image description here

, VC, a) , b) 3 . DIRECT , "a".

+4
4

, , .

  • tabbarcontroller . .
  • , .
  • .
  • segue .

:

Screen shot

, VC . , tabbarcontroller, self.parentViewController.tabBarController self.tabBarController . VC .

+12

, .

:

Storyboard

Navigation Controller Identity Inspector Restoration ID :

enter image description here

ViewController :

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationItem.title = parent?.restorationIdentifier
    label.text = parent?.restorationIdentifier
}

, , parent?.restorationIdentifier

, Navigation TopBar ViewController, None Navigation Controller, :

enter image description here

! , .

+5

, . , , StoryBoard, View 2. 1

: (.m ad.h), .

: , "" (.. ) , " " (.. UIView).

0

, . Table View Controller . , ; enter image description here

"ID ".

"navCont1" "navCont2" . enter image description here

In a subclass ("GeneralNavCont") of these navigation controllers; I override the init method and check the recovery identifier self. Then I launch my TableViewController and set its data source based on these identifiers:

class GeneralNavCont: UINavigationController {

   var dataSource1 = [Countries]()
   var dataSource2 = [Cities]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

    }

     required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            initiateTableVCBasedOnId()
        }

      func initiateTableVCBasedOnId() {
            let storyBoard = UIStoryboard(name: "Main", bundle: nil)
            let tableVC = storyBoard.instantiateViewController(withIdentifier: "tableVC") as! MyTableViewController

            if self.restorationIdentifier == "navCont1" {
                tableVC.dataSource = self.dataSource1
                self.viewControllers = [tableVC]
            }
            else if self.restorationIdentifier == "navCont2" {
                tableVC.dataSource = self.dataSource2
                self.viewControllers = [tableVC]
            }

        }
}

Hope this helps someone. Greetings.

0
source

Source: https://habr.com/ru/post/1532373/


All Articles