Search code examples
iosswiftxcodeuinavigationcontroller

In navigation controler how to implement a add button opens a new view controller


I am new to swift , I have a very simple question. I implement a navigation controller with two items at top like this enter image description here

I did this by adding a navigation controller to the project and then adding this lines of code in to the viewDidLoad

 override func viewDidLoad() {
    super.viewDidLoad()

    self.title = ""
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.setLeftBarButton(UIBarButtonItem(barButtonSystemItem: .add, target: self, action: Selector(("barButtonItemClicked:"))), animated: true)
  }

now my question is about how to open a new view controller I mean a new page after clicks on plus (+) button at the navigation bar. I searched a lot but did not find any exact thing relate to this. Appreciate you if possible help me. thank you


Solution

  • Here is code for Push TO OTherView Controller

    @objc func PoushTOHistoryVC() {
    
        let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "HistoryViewController") as! HistoryViewController
        self.navigationController?.pushViewController(secondViewController, animated: true)
    }
    

    here is Button for Add

     self.navigationItem.setLeftBarButton(UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(PoushTOHistoryVC)), animated: true)
    

    you need to add HistoryViewControlle to Stroyboard ViewController HistoryViewControlle and Don't forget to tick