简体   繁体   中英

SWIFT 4 Present viewcontroller

I am trying to building a app using the master details template. in the Master view controller I added a button called Catalogue : this button showing a tabbar controller called Catalogue.

在此处输入图片说明

I don't use Segue to show the catalogue, I use the code below to show the tab controller

From Master form I called the Tabbar controller :

@IBAction func Btn_Catalogue(_ sender: Any) {
   let AddCatalogueVC = self.storyboard?.instantiateViewController(withIdentifier: "CatalogueVC") as! CatalogueVC
   present(AddCatalogueVC, animated: true, completion: nil)
}

From CategorieVC I use the code below to show

@IBAction func Btn_AddCategorie(_ sender: Any) {
    self.Mode = "New"
    let AddCategorieViewController = self.storyboard?.instantiateViewController(withIdentifier: "AddCategorieVC") as! AddCategorieVC
    present(AddCategorieViewController, animated: true, completion: nil)
}

I dismiss the AddCategorieVC using the code below

  @IBAction func Btn_Save(_ sender: Any) 
    {
        if self.Txt_CategorieName.text  != ""{
            self.Mysave = true
            self.MyCategorieName = self.Txt_CategorieName.text!
            self.dismiss(animated: true, completion: nil)
           }
        }

I have unwind SEGUE from Save button to a function in categorieVC

 @IBAction func FctSaveCategories(_ sender: UIStoryboardSegue) {
  let sendervc = sender.source as! AddCategorieVC
        if self.Mode == "New" && sendervc.Mysave == true  { // Mode insert



            let MyCategories = TblCategorie(context: Mycontext)
            MyCategories.categorie_Name = sendervc.MyCategorieName
             do {
              try Mycontext.save()
    } catch {
        debugPrint ("there is an error  \(error.localizedDescription)")
    }
}
}

The problem is when I hit the save button in categorieVC the catalogueVC is also dismissing at the same time returning me to the master control.

I am almost sure that the problem came from the Unwind segue but I don't know why.

Update : I activate the Cancel button in AddCategorieVC with the code below

  self.dismiss(animated: true, completion: nil)

and when I clicked on it only the AddCategorieVC is being dismissed and I go back to CatalogueVC. The difference between the save button and the Cancel Button is only the UNWIND segue on the Save Button.

And when I add UnWIND segue to the cancel Button (just to test the behavior) it took me back to the master form instead CatalogueVC.

How can I solve that?

And yesss I found it

It look like that unwind segue automaticly handled dismiss contrôle

So all I need to do is remove the dismiss code from the save button this way the unwind segue will took me back to catalogueVC.

.

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