Ios UIAlert和View控制器的错误

Ios UIAlert和View控制器的错误,ios,swift,ipad,alert,Ios,Swift,Ipad,Alert,在我的iOS iPad应用程序中,我有一个非常奇怪的bug,它的默认值是Alert 在它上面我有三个按钮:一个是相机按钮,第二个是照片库按钮,第三个是关闭按钮。警报用于拾取照片。我遇到的问题是,有时,当我单击该警报上的Disclose按钮时,会执行以下类型的代码: let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginPageViewCont

在我的iOS iPad应用程序中,我有一个非常奇怪的bug,它的默认值是
Alert

在它上面我有三个按钮:一个是相机按钮,第二个是照片库按钮,第三个是关闭按钮。警报用于拾取照片。我遇到的问题是,有时,当我单击该警报上的Disclose按钮时,会执行以下类型的代码:

let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginPageViewController
我不确定这是否就是get执行的代码,但它执行注销操作,用户被重定向到登录屏幕

这是我的警报代码

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)
    }
正如您所看到的,警报中的代码没有任何注销操作,但有时会发生这种情况。也许有人也有类似的问题?原因可能是什么

如果你需要更多信息,请告诉我。提前感谢您的帮助

试试看

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

取消操作中关闭当前视图控制器的问题。将其样式设置为不带处理程序的“取消”

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

首先,您不需要为.cancel类型操作按钮编写任何代码来关闭显示的警报视图控制器。其次,您可以使用view来显示警报控制器,而无需要求其父级(tabBarController)来执行此操作。您在.cancel按钮中的代码将关闭登录控制器本身

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() { }

}

首先,您不需要为
编写任何代码。取消
键入操作按钮以关闭显示的警报视图控制器。其次,您只需使用
视图
来显示警报控制器,而无需要求它的父级(tabBarController)执行此操作。您在
.cancel
按钮中的代码将关闭登录控制器本身。感谢您的建议。我会试试看它是怎么工作的。我使用
(查看为?UIViewController)?.tabBarController!的原因!。当前(alertController,动画:true,完成:nil)
是我在我的应用程序中使用了VIPER,并且该
警报
逻辑在Presenter中。我已经测试了您的解决方案一段时间,该错误不再出现,所以我认为现在已经解决了。谢谢你的帮助!