swift - 弹出窗口在代码中的警报内部不起作用

标签 swift popup alert

我正在通过 TableView 中的尾随滑动创建警报。 单击“编辑”时,我尝试实例化另一个 View Controller 并将其显示为弹出窗口。

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

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

在不同的屏幕上,当我将 View Controller 设置为从 Storyboard 中显示为弹出窗口时,这非常有效,但我不知道如何让它在警报内部工作。

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

到目前为止我尝试过的:

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,animated: false)

4) 无需导航 Controller 即可 self 呈现。

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")
            })

最佳答案

在进一步阅读并尝试多个选项时,我发现此问题不是由于从警报内部启动的弹出窗口所致。

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

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

presentViewController and displaying navigation bar

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

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

关于swift - 弹出窗口在代码中的警报内部不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58789729/

相关文章:

ios - 将触摸转发到 UIPageViewController

ios - 以编程方式创建一个在设备旋转时保持其大小的 UIView

email - 更改 TFS 2015 电子邮件警报中的主题

javascript - 在警告框中转储 jquery 对象

swift - 在 Swift 中将类型动态传递给 MemoryLayout<T>

ios - 清除 CAShapeLayer 中的所有内容

javascript - 如何防止打开 IE 中的默认选项?

javascript - javascript中的自定义弹出窗口

javascript - 使用 jAlerts 动态更改按钮文本

swift - URL 请求的编码参数