简体   繁体   English

UIAlert和View Controller的错误

[英]Bug with UIAlert and View Controller

I have very strange bug with default Alert in my iOS app for iPad. 我在iPad的iOS应用中使用默认Alert存在一个非常奇怪的错误。

On it I have three buttons: one for camera, second for photo gallery and third is dismiss button. 在它上面有三个按钮:一个用于相机,第二个用于照片库,第三个是关闭按钮。 Alert is used to pick photos. 警报用于拾取照片。 Issue I have is sometimes, when I click on dismiss button on that alert, this type of code get's executed: 有时候,我遇到的问题是,当我单击该警报上的关闭按钮时,将执行以下类型的代码:

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

I'm not sure if this is exactly the code that get's executed, but it performs logout action and users get redirected to login screen. 我不确定这是否就是要执行的代码,但是它执行注销操作,并且用户会重定向到登录屏幕。

Here's my code for Alert 这是我的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)
    }

As you can see, my code in alert doesn't have any actions for logout, but sometimes such thing happens. 如您所见,alert中的代码没有任何注销操作,但有时会发生这种情况。 Maybe someone had similar issues? 也许有人遇到过类似的问题? What could be the reason for that? 可能是什么原因呢?

If you need more information - let me know. 如果您需要更多信息,请告诉我。 Thanks in advance for your help! 在此先感谢您的帮助!

First you don't need to write any code for a .cancel type action button to dismiss the presented alert view controller. 首先,您不需要为.cancel类型的动作按钮编写任何代码即可关闭显示的警报视图控制器。 Secondly, you can just use view to present the alert controller, no need to ask it's parent (tabBarController) to do it. 其次,您可以仅使用视图来显示警报控制器,而无需要求其父级(tabBarController)来执行。 Your code in the .cancel button dismiss the Login controller itself. 您在.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)

The issue in cancelAction which dismiss the current view controller. cancelAction中的问题将当前视图控制器关闭。 set its style to cancel with no handler. 将其样式设置为无需处理程序即可取消。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM