Chien-an Lin
Chien-an Lin

Reputation: 31

IOS navigationController?.popToRootViewController has wrong

@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

Upvotes: 1

Views: 930

Answers (2)

devang bhatt
devang bhatt

Reputation: 470

_ = 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.

Upvotes: 1

anshul king
anshul king

Reputation: 568

Because it returns Bool

_ = navigationController?.popToRootViewController(animated: true)

Upvotes: 1

Related Questions