呈现UIAlertController时应用程序崩溃

时间:2017-11-08 09:48:37

标签: ios swift ios11 uialertcontroller

我的应用在尝试呈现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则没有。

为什么会发生这种情况

2 个答案:

答案 0 :(得分:5)

所以我找出了问题。

不幸的是,Xcode对他们的崩溃日志没有帮助,这些日志很模糊。

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

为了做到这一点,我添加了以下代码,一切都按预期工作:

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)

答案 1 :(得分:0)

提醒PopUp

这对我有帮助,我觉得这对你有帮助

     let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.alert)

    refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
        print("Handle Ok logic here")
    }))

    refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
        print("Handle Cancel Logic here")
    }))

    present(refreshAlert, animated: true, completion: nil)

操作表

    let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

     create an action
       let firstAction: UIAlertAction = UIAlertAction(title: "First Action", style: .default) { action -> Void in

            print("First Action pressed")
        }

        let secondAction: UIAlertAction = UIAlertAction(title: "Second Action", style: .default) { action -> Void in

            print("Second Action pressed")
        }

        let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in }

    // add actions
    actionSheetController.addAction(firstAction)
    actionSheetController.addAction(secondAction)
    actionSheetController.addAction(cancelAction)

     present an actionSheet...
    present(actionSheetController, animated: true, completion: nil)

}