UIAlert 和视图控制器的错误
Bug with UIAlert and View Controller
我的 iOS 应用中 iPad 的默认值 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中的代码没有任何注销操作,但有时会发生这种情况。也许有人有类似的问题?这可能是什么原因?
如果您需要更多信息 - 让我知道。预先感谢您的帮助!
尝试
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
取消当前视图控制器的 cancelAction 中的问题。将其样式设置为不使用处理程序取消。
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
首先,您无需为 .cancel 类型的操作按钮编写任何代码即可关闭显示的警报视图控制器。其次,您可以只使用视图来呈现警报控制器,无需请求它的父级 (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() { }
}
我的 iOS 应用中 iPad 的默认值 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中的代码没有任何注销操作,但有时会发生这种情况。也许有人有类似的问题?这可能是什么原因?
如果您需要更多信息 - 让我知道。预先感谢您的帮助!
尝试
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
取消当前视图控制器的 cancelAction 中的问题。将其样式设置为不使用处理程序取消。
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
首先,您无需为 .cancel 类型的操作按钮编写任何代码即可关闭显示的警报视图控制器。其次,您可以只使用视图来呈现警报控制器,无需请求它的父级 (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() { }
}