出现UIAlertController时应用崩溃 [英] App crash when presenting UIAlertController

查看:80
本文介绍了出现UIAlertController时应用崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试呈现 UIAlertController 时,我的应用程序崩溃.

My app's crashing when trying to present a UIAlertController.

我有一个 UIViewController 以模态显示,然后在该显示的视图控制器上,当点击某个按钮时,我想显示一个 actionSheet 警报.

I have a UIViewController that gets presented modally, then on that presented view controller when a certain button is tapped I want to present a actionSheet alert.

这样做会导致应用程序崩溃,我无法弄清原因.

Somehow the app crashes when doing this and I cannot figure it out why.

代码如下:

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "Image from Camera", style: .default, handler: { (_) in
    let cameraController = CameraController()
    self.present(cameraController, animated: true, completion: nil)
}))
alertController.addAction(UIAlertAction(title: "Image from Library", style: .default, handler: {(_) in
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.allowsEditing = true
    self.present(imagePickerController, animated: true, completion: nil)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.view.tintColor = UIColor.rgb(red: 46, green: 94, blue: 120)
self.present(alertController, animated: true, completion: nil)

崩溃日志:

libc ++ abi.dylib:以类型为NSException的未捕获异常终止

libc++abi.dylib: terminating with uncaught exception of type NSException

当我 backtrace 时,错误显示如下:

When I backtrace the error it shows this:

    * thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00000001101a7fce libsystem_kernel.dylib`__pthread_kill + 10
    frame #1: 0x00000001101e1150 libsystem_pthread.dylib`pthread_kill + 333
    frame #2: 0x000000010fe650e7 libsystem_c.dylib`abort + 127
    frame #3: 0x000000010fbf791f libc++abi.dylib`abort_message + 245
    frame #4: 0x000000010fbf7abb libc++abi.dylib`default_terminate_handler() + 265
    frame #5: 0x0000000109b341be libobjc.A.dylib`_objc_terminate() + 97
    frame #6: 0x000000010fc13159 libc++abi.dylib`std::__terminate(void (*)()) + 8
    frame #7: 0x000000010fc12e0a libc++abi.dylib`__cxa_rethrow + 99
    frame #8: 0x0000000109b340dc libobjc.A.dylib`objc_exception_rethrow + 40
    frame #9: 0x000000010a424a39 CoreFoundation`CFRunLoopRunSpecific + 537
    frame #10: 0x0000000112faf9c6 GraphicsServices`GSEventRunModal + 62
    frame #11: 0x000000010bd325e8 UIKit`UIApplicationMain + 159
    frame #13: 0x000000010fd92d81 libdyld.dylib`start + 1
    frame #14: 0x000000010fd92d81 libdyld.dylib`start + 1

我什至试图在主队列上分发演示文稿,但仍然无法正常工作.

I even tried to dispatch on main queue the presentation, but it still doesn't work.

有任何提示吗?

谢谢.

编辑

为了过滤一些问题,我已经实现了一个简单的警报控制器,但使用 .actionSheet 仍然会崩溃,但使用 .alert

In order to filter some problems I've implemented a simple alert controller and it still crash using .actionSheet but not with .alert

let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.actionSheet)
present(refreshAlert, animated: true, completion: nil)

这样做会导致崩溃,但是如果我使用 .alert 则不会.

Doing this it crashes, but if I use .alert it does not.

为什么会这样

推荐答案

所以我找出了问题所在.

So I figure out the problem.

不幸的是,Xcode对其模糊的崩溃日志没有帮助.

Unfortunately Xcode doesn't help with their crash logs, which are vague.

问题在于,由于我正在iPad上测试此 UIAlertController ,因此我应该实现警报控制器的源视图.

The problem is that since I'm testing this UIAlertController on an iPad I should implement the alert controller's source view.

为此,我添加了以下代码,并且一切都按预期工作:

In order to do so, I've added the following code and everything is working as expected:

if UIDevice.current.userInterfaceIdiom == .pad {
    guard let button = self.header?.profileImageButton else { return }
    alertController.popoverPresentationController?.permittedArrowDirections = .right
    alertController.popoverPresentationController?.sourceView = button
}
self.present(alertController, animated: true, completion: nil)

这篇关于出现UIAlertController时应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆