当我尝试展示UIAlertController
时,我的应用程序崩溃了。
我有一个UIViewController
以模态方式呈现,然后在呈现的视图控制器上,当点击某个按钮时,我想展示一个actionSheet
警报。
不知何故,在执行此操作时应用程序会崩溃,我无法弄清楚原因。
以下是代码:
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 的未捕获异常
当我使用 backtrace
函数跟踪错误时,显示如下内容:
* 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
我甚至尝试在主队列上调度演示,但仍然无法工作。有任何提示吗?谢谢。
编辑:为了过滤一些问题,我实现了一个简单的警报控制器,并且仍然会在使用.actionSheet时崩溃,但不会在使用.alert时崩溃。
let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.actionSheet)
present(refreshAlert, animated: true, completion: nil)
使用这种方式会导致崩溃,但如果使用.alert
则不会出现此问题。
为什么会发生这种情况?