ios - UIAlert 和 View Controller 的错误

标签 ios swift ipad alert

我的 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 类型的操作按钮编写任何代码来关闭显示的警报 View Controller 。其次,您可以只使用 View 来呈现警报 Controller ,无需请求它的父级 (tabBarController) 来执行此操作。您在 .cancel 按钮中的代码关闭了登录 Controller 本身。

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

}

关于ios - UIAlert 和 View Controller 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53170861/

相关文章:

ios - 如何快速检查使用 alamofire 创建的类模型是否为 nil?

iphone - 无法使用另一个类的 setHidden 属性隐藏控件?

c# - iOS:缺少条形样式外观的 Monotouch 绑定(bind)?

swift - 复制 Array.reduce() 方法

ios - 如何处理 - (BOOL)application :(UIApplication *)app openURL:(NSURL *)url in storyboard application?

ios - 在自定义框架中访问应用程序的捆绑标识符

ios - 将数据从第二个 VC 传递到第一个 VC 并显示在文本字段 Swift4 中

html - 当您在 iPad 上的 Safari 中触摸 HTML 元素时,它会变成灰色。决定嵌套元素中哪个元素显示为灰色的逻辑是什么?

objective-c - 在 UITableView 中选择单元格时创建 UIWebView

ios - UITableViewCell 的自动布局问题