Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.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

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 我可以从IBAction中调用UIAlertController吗_Ios_Swift_Uialertcontroller_Ibaction - Fatal编程技术网

Ios 我可以从IBAction中调用UIAlertController吗

Ios 我可以从IBAction中调用UIAlertController吗,ios,swift,uialertcontroller,ibaction,Ios,Swift,Uialertcontroller,Ibaction,我正试图在我的应用程序上发出警报,但它一直像下面一样给我警告,而且它没有出现 Warning: Attempt to present <UIAlertController: 0x..> on <xyz.VC1: 0x..> whose view is not in the window hierarchy! 您可能需要让函数返回警报,而不是显示警报,然后从VC1显示警报(self.present)。正如Teja Nandamuri在评论中提到的,必须从可见的UIVie

我正试图在我的应用程序上发出警报,但它一直像下面一样给我警告,而且它没有出现

Warning: Attempt to present <UIAlertController: 0x..> on <xyz.VC1: 0x..> whose view is not in the window hierarchy! 

您可能需要让函数返回警报,而不是显示警报,然后从VC1显示警报(
self.present
)。正如Teja Nandamuri在评论中提到的,必须从可见的
UIViewController
发出警报

根据评论修订: 您可以在单独的函数(如WAlert)中生成警报,但仍然可以在VC1中显示警报,即使在IBAction中也是如此。例如,在行动中,您将:

let alert = WAlert()
self.present(alert, animated: true)
您需要按如下方式更改WAlert:

func WAlert() -> UIAlertController {
    let alert = UIAlertController(title: "S?", message: "Y", preferredStyle: UIAlertController.Style.alert)

    alert.addAction(UIAlertAction(title: "C", style: UIAlertAction.Style.default, handler: { _ in
        //Cancel Action
    }))
    alert.addAction(UIAlertAction(title: "out", style: UIAlertAction.Style.default, handler: {(_: UIAlertAction!) in
        //Sign out action
    }))
    return alert

请添加您如何显示警报的代码。您能告诉我们使用的代码吗?此外,调用此公共函数是否涉及任何异步线程?您是否尝试过在VC1中定义函数并以这种方式调用它?您只能在用户可见的视图控制器上显示警报。它将以这种方式工作,但我正在尝试避免“意大利面代码”,我正在尝试使代码简单、易于维护和编辑,以便进一步更新,这就是为什么我为“函数”创建单独的文件“视图”“警报”“…等等,你这么说可能太早了。除非显示控制器已出现,否则无法显示控制器。这意味着您不能从
viewDidLoad
viewwillbeen
中显示它。只有在调用了
viewdide
后,才能从
self
显示控制器。我已尝试显示警报,但结果表明警报仅在iAction内有效(或我的警报)
func WAlert() -> UIAlertController {
    let alert = UIAlertController(title: "S?", message: "Y", preferredStyle: UIAlertController.Style.alert)

    alert.addAction(UIAlertAction(title: "C", style: UIAlertAction.Style.default, handler: { _ in
        //Cancel Action
    }))
    alert.addAction(UIAlertAction(title: "out", style: UIAlertAction.Style.default, handler: {(_: UIAlertAction!) in
        //Sign out action
    }))
    return alert