gpt4 book ai didi

ios - 我如何普遍引用 self.view?

转载 作者:行者123 更新时间:2023-11-28 13:26:49 25 4
gpt4 key购买 nike

我有一个可重用的 alertController 组件,该组件被引用以在整个应用程序中创建一个 alertController。我正在尝试添加模糊效果,但我无法引用 alertController 始终位于其上的 View 。

let blurEffect = UIBlurEffect(style: .light)
let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)

blurVisualEffectView.frame = self.view.bounds

let vc = UIAlertController(title: title, message: message, preferredStyle: .alert)

let alertActions = (actions.map { a in UIAlertAction(title: a.display, style: .default, handler: { _ in then() ; a.handler?() }) })

for action in alertActions {
vc.addAction(action)
blurVisualEffectView.removeFromSuperview()
}

if actions.isEmpty {
let action = UIAlertAction(title: "Something went wrong", style: .default) { _ in then() }
vc.addAction(action)
}

self.view.addSubview(blurVisualEffectView)

我得到的错误是

Use of unresolved identifier 'self'

最佳答案

你需要

extension UIViewController {

func showBluredAlert(_ actions:[Model]) {
let blurEffect = UIBlurEffect(style: .light)
let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
blurVisualEffectView.frame = self.view.bounds
self.view.addSubview(blurVisualEffectView)

// add the alert here

}
}

然后在任何 vc 实例中调用它

showBluredAlert(<#[Model]#>)

也不要忘记这一行

self.present(vc, animated: true, completion: nil)

关于ios - 我如何普遍引用 self.view?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58223018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com