簡體   English   中英

UIAlert和View Controller的錯誤

[英]Bug with UIAlert and View Controller

我在iPad的iOS應用中使用默認Alert存在一個非常奇怪的錯誤。

在它上面有三個按鈕:一個用於相機,第二個用於照片庫,第三個是關閉按鈕。 警報用於拾取照片。 有時候,我遇到的問題是,當我單擊該警報上的關閉按鈕時,將執行以下類型的代碼:

let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginPageViewController

我不確定這是否就是要執行的代碼,但是它執行注銷操作,並且用戶會重定向到登錄屏幕。

這是我的Alert代碼

func showPopover(uiView: UIView) {
        let alertController = UIAlertController(title: nil, message: "Choose Photos", preferredStyle: .actionSheet)

        let defaultAction = UIAlertAction(title: "Camera", style: .default, handler: { (alert: UIAlertAction!) -> Void in
            self.view?.pickPhotoFromCamera()
        })

        let galleryAction = UIAlertAction(title: "Gallery", style: .default, handler: { (alert: UIAlertAction!) -> Void in
            self.view?.pickPhotoFromLibrary()
        })

        let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: { (alert: UIAlertAction!) -> Void in
            (self.view as? UIViewController)?.dismiss(animated: true, completion: nil)
        })

        alertController.addAction(defaultAction)
        alertController.addAction(galleryAction)
        alertController.addAction(cancelAction)

        if let popoverController = alertController.popoverPresentationController {
            popoverController.sourceView = uiView
            popoverController.sourceRect = CGRect(x: uiView.bounds.midX, y: uiView.bounds.midY, width: 0, height: 0)
            popoverController.permittedArrowDirections = []
        }
        (view as? UIViewController)?.tabBarController!.present(alertController, animated: true, completion: nil)
    }

如您所見,alert中的代碼沒有任何注銷操作,但有時會發生這種情況。 也許有人遇到過類似的問題? 可能是什么原因呢?

如果您需要更多信息,請告訴我。 在此先感謝您的幫助!

首先,您不需要為.cancel類型的動作按鈕編寫任何代碼即可關閉顯示的警報視圖控制器。 其次,您可以僅使用視圖來顯示警報控制器,而無需要求其父級(tabBarController)來執行。 您在.cancel按鈕中的代碼關閉了Login控制器本身。

import UIKit

class LoginViewController: UIViewController {

  func showPopover(uiView: UIView) {

    let alertController = UIAlertController(title: nil, message: "Choose Photos", preferredStyle: .actionSheet)


    let defaultAction = UIAlertAction(title: "Camera", style: .default, handler: { (alert: UIAlertAction!) -> Void in
      self.pickPhotoFromCamera()
    })

    let galleryAction = UIAlertAction(title: "Gallery", style: .default, handler: { (alert: UIAlertAction!) -> Void in
      self.pickPhotoFromLibrary()
    })

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

    alertController.addAction(defaultAction)
    alertController.addAction(galleryAction)
    alertController.addAction(cancelAction)

    if let popoverController = alertController.popoverPresentationController {
      popoverController.sourceView = uiView
      popoverController.sourceRect = uiView.bounds
    }

    present(alertController, animated: true, completion: nil)

  }

  private func pickPhotoFromCamera() { }
  private func pickPhotoFromLibrary() { }

}

嘗試

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

cancelAction中的問題將當前視圖控制器關閉。 將其樣式設置為無需處理程序即可取消。

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM