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 如何从选项卡栏控制器弹出或以模式显示视图控制器?_Ios_Swift_Uitabbarcontroller_Uimodalpresentationstyle - Fatal编程技术网

Ios 如何从选项卡栏控制器弹出或以模式显示视图控制器?

Ios 如何从选项卡栏控制器弹出或以模式显示视图控制器?,ios,swift,uitabbarcontroller,uimodalpresentationstyle,Ios,Swift,Uitabbarcontroller,Uimodalpresentationstyle,当按下选项卡栏上的按钮时,如何创建弹出窗口?我想要类似的东西: 我尝试过许多解决方案,但没有一个奏效 例如,我在我的主视图控制器上尝试过: popupViewController.modalPresentationStyle = .fullScreen tabBarController.present(popupViewController, animated: true) 但这仍然不起作用。我将如何着手创建这个。我知道我需要在当前上下文中以模态的方式呈现视图控制器,但是如何从选项卡栏控制器中

当按下选项卡栏上的按钮时,如何创建弹出窗口?我想要类似的东西:

我尝试过许多解决方案,但没有一个奏效

例如,我在我的主视图控制器上尝试过:

popupViewController.modalPresentationStyle = .fullScreen
tabBarController.present(popupViewController, animated: true)

但这仍然不起作用。我将如何着手创建这个。我知道我需要在当前上下文中以模态的方式呈现视图控制器,但是如何从选项卡栏控制器中实现这一点

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if viewController is PopupViewController {
        if let popupView = tabBarController.storyboard?.instantiateInitialViewController() {
            popupView.modalPresentationStyle = .fullScreen
            tabBarController.present(popupView, animated: true, completion: nil)

            return false
        }
    }
    return true
}
以下是一些可能有帮助的图片:

主情节提要:

弹出故事板:

视图控制器代码:


您是否尝试过在Xcode中使用断点来调试它?从我看到的情况来看,您要做的第一件事是检查应该选择的视图控制器是否属于类
PopupViewController
。您确定视图控制器的实例化正确吗

顺便说一下,我推荐另一种从故事板实例化视图控制器的方法,而不是:

tabBarController.storyboard?.instantiateInitialViewController()
第一件事是转到情节提要文件本身,并在视图控制器中尝试实例化将
情节提要ID
更改为例如情节提要的类(
PopupViewController

接下来,您将要尝试使用
init(名称:String,bundle故事板bundleornil:bundle?)实例化故事板本身
initialiser:

let storyboard = UIStoryboard(name: "Popup", bundle: nil)
现在使用
序列图像板
变量实例化视图控制器,如下所示:

let popupViewController  = storyboard.instantiateViewController(withIdentifier: "PopupViewController") as! PopupViewController
最后,您可以为其提供一些额外的配置,并将其显示在选项卡栏控制器上:

popupViewController.modalPresentationStyle = .fullScreen
tabBarController.present(popupViewController, animated: true)
编辑

另外,为了让它更快捷,我建议提前退出
guard
语句。最后,该方法可以如下所示:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    guard viewController is PopupViewController else { return true }
    let storyboard = UIStoryboard(name: "Popup", bundle: nil)
    let popupViewController = storyboard.instantiateViewController(withIdentifier: "PopupViewController") as! PopupViewController
    popupViewController.modalPresentationStyle = .fullScreen
    tabBarController.present(popupViewController, animated: true, completion: nil)
    return false
}

“我知道我需要在当前上下文中以模式显示视图控制器”事实上,不是。在您提到的YouTube视频中,这不是当前上下文显示:这是一个“全屏”显示。它可能使用一个定制的显示控制器来设置显示视图的大小/位置(带有测试和完成的蓝色方框)。我得到一个线程1:信号SIGABRT错误,但没有断开的出口。它位于应用程序代理文件中。完整的错误消息是什么?你能在github或其他地方分享这个项目吗,这样我就可以自己运行它,以便更好地检查?我想告诉你,这只是一个测试项目,这样我就可以将这个功能合并到一个更大的应用程序中。好的,所以导致崩溃的是
Main.storyboard
中的引用。单击它,然后弹出
作为参考情节提要。在你改变了这一点之后,一切都应该按预期进行。这几乎成功了,我不敢相信我错过了这一点;然而,当弹出窗口“弹出”时,背景起初是透明的,显示后面的内容,但随后变成纯黑色。这是一段视频