【问题标题】:IOS navigationController?.popToRootViewController has wrongIOS navigationController?.popToRootViewController 有错误
【发布时间】:2017-04-10 03:58:34
【问题描述】:
@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)
}

我想创建一个新的简单编辑页面。对我来说有两个问题,当我完成编辑时 if-else 会检查条件是否正确,然后保存数据 popToRootViewControlle。当我完成编辑时,我点击“addInformation”BarButtonItem,我得到了 UI 错误。另一个错误是当我点击 editField 但我没有输入任何条件时。然后我点击“cannelInformationPage”UIBarButtonItem。它也会出错。

It's what I get wrong

wrong information

【问题讨论】:

  • 不清楚我的用户界面错误是什么意思?如果您的 roor 控制器应该显示来自 UserDefaults 的更新信息,那么您应该手动刷新它,例如在 viewWillAppear
  • i.stack.imgur.com/E2tmE.png 我的意思是当我点击“cannelInformationPage”或“addInformation”时。代码应该运行“navigationController?.popToRootViewController(animated: true)”。但它推错了
  • 您的应用程序崩溃了吗?根据附图,您的应用程序由于某些原因而崩溃,而不是用户界面错误。
  • @Chien-anLin 你能从调试终端显示错误吗?在图像上只有一部分堆栈跟踪
  • i.stack.imgur.com/gTttq.png 我收到这个错误信息

标签: ios swift uiviewcontroller navigationcontroller poptoviewcontroller


【解决方案1】:

因为它返回 Bool

_ = navigationController?.popToRootViewController(animated: true)

【讨论】:

    【解决方案2】:
    _ = self.navigationController?.popViewController(animated: true)
    

    如果你想回到最后一个视图控制器而不是你可以尝试上面的行,希望它对你有用。

    【讨论】:

    • 我尝试添加自我,但它不起作用。如果我把所有的 if-else 都去掉,它就可以工作了
    • 你试过 popViewController(animated: true) 吗?
    • 按下UIBarButtonItem需要返回View Controller吗?那你一定要试试,@IBAction func cannelInformationPage(_ sender: UIBarButtonItem) { _ = navigationController?.popToRootViewController(animated: true) }
    • 请检查@IBAction 方法是否正确连接?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多