Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 显示UIAlertController时应用程序崩溃_Ios_Swift_Ios11_Uialertcontroller - Fatal编程技术网

Ios 显示UIAlertController时应用程序崩溃

Ios 显示UIAlertController时应用程序崩溃,ios,swift,ios11,uialertcontroller,Ios,Swift,Ios11,Uialertcontroller,尝试显示UIAlertController时,我的应用程序崩溃 我有一个UIViewController,它以模块方式呈现,然后当点击某个按钮时,在呈现的视图控制器上,我想呈现一个actionSheet警报 不知怎的,这个应用程序在这样做的时候崩溃了,我不知道为什么 代码如下: let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) alertControlle

尝试显示
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类型的未捕获异常终止

当I
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
则不会


为什么会出现这种情况

警报弹出窗口

    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)

}
这是帮我的我想这是帮你的

     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)

}

所以我找到了问题所在

不幸的是,Xcode不能帮助处理他们的崩溃日志,这些日志很模糊

问题是,既然我在iPad上测试这个
UIAlertController
,我就应该实现alert控制器的源代码视图

为此,我添加了以下代码,一切正常:

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)

我不知道问题是否在于从已经呈现的视图控制器中呈现警报控制器;但我以前做过任何问题检查这段代码是在后台线程中还是从后台线程调用的。@Nitish正如我提到的,我试图在主线程中显示它,但它仍然崩溃。可能相关,也可能无关。。。我注意到的一件事是UIColor.rgb()。这是您自己的UIColor自定义扩展方法吗?因为通常提供的颜色值介于0和1之间?幸运的是,问题可能不是UIColor扩展,但我刚刚尝试使用默认颜色,但它仍然崩溃。@seggy:编辑此项不是一项好工作:)我猜您是StackOverflow新手。你分享的答案不是OP想要的答案。OP已经知道你的建议,但他面临的问题在于代码。它也可能与您编写的代码相同。