【问题标题】:Alert Box Is not showing. X Code is Skip the alert box code in Swift警报框未显示。 X 代码是跳过 Swift 中的警报框代码
【发布时间】:2021-08-13 04:43:18
【问题描述】:

当我在ViewController2performSegue 然后在ViewController1 中自动调用unwindToMyHours。 那么我不知道为什么 X Code 会跳过呈现Alert BOX 的代码。

OUTPUT -> 其他部分完成。

和 xCode 跳过 -> Task 1task 2

~ViewController2的代码

@IBAction func CancelButtonTapped(_ sender: UIButton) {
    let alert = UIAlertController(title: "Delete Time", message: "Are you sure?", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { [self] _ in
            
            self.deleted = true
            self.performSegue(withIdentifier: "unwindToMyHours", sender: nil)
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: true, completion: nil)
 }

~ViewController1的代码

@IBAction func unwindToMyHours( _ seg: UIStoryboardSegue) {
    if (seg.source as? ViewController2)?.submitted == true {
        loadData()
    } else if (seg.source as? ViewController2)?.deleted == true {
        let alert = UIAlertController(title: "Deleted", message: "Tabel have been updated", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { alert in
            print("Task 1")
        }))
        present(alert, animated: true) {
            print("Task 2")
        }
        print("Else part complete")
    }
}

【问题讨论】:

    标签: swift xcode segue uialertview uialertcontroller


    【解决方案1】:

    嗯。 也许您忘记销毁您的第一个警报控制器。

    在不破坏第一个警报控制器的情况下,您呈现第二个警报控制器,因此 XCode 无法呈现您的第二个警报控制器。

    你的代码看起来不错……让我编辑一下。

    ~ViewController2的代码

    @IBAction func CancelButtonTapped(_ sender: UIButton) {
        let alert = UIAlertController(title: "Delete Time", message: "Are you sure?", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { [self] _ in
            
            self.deleted = true
            self.dismiss(animated: false, completion: nil) // You are forgetting  this
            self.performSegue(withIdentifier: "unwindToMyHours", sender: nil)
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
    

    ~ViewController1 的代码 -> 相同的代码

    @IBAction func unwindToMyHours( _ seg: UIStoryboardSegue) {
    if (seg.source as? ViewController2)?.submitted == true {
        loadData()
    } else if (seg.source as? ViewController2)?.deleted == true {
        let alert = UIAlertController(title: "Deleted", message: "Tabel have been updated", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { alert in
            print("Task 1")
        }))
        present(alert, animated: true) {
            print("Task 2")
        }
        print("Else part complete")
    }
    }
    

    【讨论】:

      【解决方案2】:

      这很可能是因为您之前的视图控制器还没有被解雇,并且您不能present 一个新的视图控制器(警报是),直到您的视图控制器成为顶部。等到发生这种情况:

      var shouldDisplayDeletedAlert = false
      
      override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated)
          if shouldDisplayDeletedAlert {
              let alert = UIAlertController(title: "Deleted", message: "Tabel have been updated", preferredStyle: .alert)
              alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { alert in
                  print("Task 1")
              }))
              present(alert, animated: true) {
                  print("Task 2")
              }
              shouldDisplayDeletedAlert = false
          }
      }
      
      @IBAction func unwindToMyHours( _ seg: UIStoryboardSegue) {
          if (seg.source as? ViewController2)?.submitted == true {
              loadData()
          } else if (seg.source as? ViewController2)?.deleted == true {
              shouldDisplayDeletedAlert = true
          }
      }
      

      【讨论】:

      • @iOSSwiftLearner is viewDidAppearunwindToMyHours 之后被调用?
      • 是的,但是 xcode 再次跳过警报视图显示的代码
      • @iOSSwiftLearner 很难说没有更多信息。警报代码看起来不错,对我有用
      猜你喜欢
      • 2011-04-19
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多