1
@IBAction func addInformation(_ sender: UIBarButtonItem) {
    // check rateHourly has value
    if let editedRateHourly = rateHourly.text {
        print("editedRateHourly condition is \(editedRateHourly)")
        if editedRateHourly != ""{
            print("not nil")
            // check edit value is number?
            let num = Int(editedRateHourly)
            if num != nil {
                print("is num")
                // add to database
                UserDefaults.standard.set(editedRateHourly, forKey: "\(findDate())")
                UserDefaults.standard.synchronize()
                // back to last viewController
                navigationController?.popToRootViewController(animated: true)
            }else{
                print("not num")
                print("error alert push!!")
                popErrorAlert()
            }
        }else {
            print("nil")
            print("editedRateHourly condition is nil")
            popErrorAlert()
        }
    }
}
@IBAction func cannelInformationPage(_ sender: UIBarButtonItem) {
    navigationController?.popToRootViewController(animated: true)
}

I want to create a new simple edit page. It's two problem for me that when I finish edition if-else will check the condition is correct or not and then save the data popToRootViewControlle. When I finish edition I click on "addInformation" BarButtonItem and I get UI wrong. the Other wrong is when I click on editField but I don't enter any condition. And then I click on "cannelInformationPage" UIBarButtonItem. It also get wrong.

It's what I get wrong

wrong information

Community
  • 1
  • 1
  • It's not clear what do you mean under **I get UI wrong**? If you roor controller should display updated information from UserDefaults then you should manually refresh it e.g on viewWillAppear – Evgeniy Gushchin Apr 10 '17 at 04:48
  • https://i.stack.imgur.com/E2tmE.png I mean when I click the "cannelInformationPage" or "addInformation".the code should be run "navigationController?.popToRootViewController(animated: true)".but it push the wrong – Chien-an Lin Apr 10 '17 at 04:58
  • Did your app crashed ? According to attached image, your app crashed due to some reasons and not wrong UI. – Surjeet Singh Apr 10 '17 at 05:02
  • @Chien-anLin could you show error from debug terminal? On image there is only part of stack trace – Evgeniy Gushchin Apr 10 '17 at 05:07
  • https://i.stack.imgur.com/gTttq.png I get this wrong message – Chien-an Lin Apr 10 '17 at 06:58
  • I have a feeling that you have an `IBOutlet/IBAction` that is not tagged correctly in your `ViewController` that's why it crashes – Zonily Jame Apr 10 '17 at 08:14

2 Answers2

1

Because it returns Bool

_ = navigationController?.popToRootViewController(animated: true)
anshul king
  • 558
  • 4
  • 17
1
_ = self.navigationController?.popViewController(animated: true)

If you want to come back to last view controller than you can try above line, hope its work for you.

devang bhatt
  • 460
  • 2
  • 15