简体   繁体   中英

Bug with UIAlert and View Controller

I have very strange bug with default Alert in my iOS app for iPad.

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

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. 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. Secondly, you can just use view to present the alert controller, no need to ask it's parent (tabBarController) to do it. Your code in the .cancel button dismiss the Login controller itself.

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. set its style to cancel with no handler.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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