首页 > 解决方案 > 弹出窗口在代码中的警报内部不起作用

问题描述

我正在从表格视图中的尾随滑动创建警报。单击编辑时,我试图实例化另一个视图控制器并将其显示为弹出窗口。

问题是弹出窗口要么显示在同一屏幕中,要么显示为弹出窗口时缺少导航栏。

我查看了 stackoverflow 上的不同线程,但找不到解决方案。

在不同的屏幕上,当我将视图控制器设置为从情节提要中显示为弹出窗口时,这非常有效,但我无法弄清楚如何从警报内部让它工作。

我添加了不起作用的代码的核心部分。很高兴添加您需要查看的任何其他详细信息。

到目前为止我尝试了什么:

1) 为 vc 设置演示样式

vc.modalPresentationStyle = .popover

2) 尝试转换——这会在同一窗口中显示 vc

            let transition = CATransition()
            transition.duration = 0.5
            transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
            transition.type = CATransitionType.reveal
            transition.subtype = CATransitionSubtype.fromTop
            self.navigationController?.view.layer.add(transition, forKey: nil)
            self.navigationController?.pushViewController(vc, animated: false)

3) 结果与#2 相同

self.navigationController?.present(vc, 动画: false)

4) 没有导航控制器的 Self.Present。

let modifyAction = UIContextualAction(style: .normal, title:  "Manage", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
            print("Update action ...")


            let alert = UIAlertController()

            alert.addAction(UIAlertAction(title: "Edit", style: .default , handler:{ (UIAlertAction)in
                    print("User click Edit button")

                let vc = self.storyboard?.instantiateViewController(withIdentifier: "ManageCategoryViewController") as! ManageCategoryViewController

                vc.selectedIdx = indexPath.row
                vc.categoryListArray = self.categoryListArray
                vc.type = "update"
                //vc.modalPresentationStyle = .popover

                /*
                let transition = CATransition()
                transition.duration = 0.5
                transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
                transition.type = CATransitionType.reveal
                transition.subtype = CATransitionSubtype.fromTop
                self.navigationController?.view.layer.add(transition, forKey: nil)
                self.navigationController?.pushViewController(vc, animated: false)
                */

                self.navigationController?.present(vc, animated: false)

            }))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{ (UIAlertAction)in
                print("User click Cancel button")
            }))

            self.present(alert, animated: true, completion: {
                print("completion block")
            })

标签: swiftpopupalert

解决方案


在进一步阅读并尝试多个选项时,我能够解决此问题不是因为弹出窗口是从警报内部启动的。

https://developer.apple.com/documentation/uikit/windows_and_screens/displaying_transient_content_in_a_popover

如果您需要导航控制器,您可以按照以下帖子中的答案之一的建议添加导航控制器。

presentViewController 和显示导航栏

发布代码以防它对某人有用。

let navController = UINavigationController(rootViewController: vc)
                vc.modalPresentationStyle = .popover
                self.navigationController?.present(navController, animated: true) {
                     // The popover is visible.
                }

推荐阅读