作者:mobiledu2502856973 | 来源:互联网 | 2023-01-09 18:12
尝试提供时,我的应用崩溃了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
它则不会崩溃。
为什么会这样
1> Ivan Cantari..:
所以我找出了问题所在。
不幸的是,Xcode对其崩溃日志没有帮助,这很模糊。
问题在于,由于我正在UIAlertController
iPad上进行测试,因此我应该实现警报控制器的源视图。
为了做到这一点,我添加了以下代码,一切都按预期工作:
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)