gpt4 book ai didi

ios - Swift 3 无法使用 UIWindow 调用函数

转载 作者:行者123 更新时间:2023-11-30 12:14:05 25 4
gpt4 key购买 nike

    let PostButton: UIButton = {
let pB = UIButton(type: .system)
pB.setTitle("Post", for: .normal)
pB.titleLabel!.font = UIFont(name: "Arial Rounded MT Bold", size:18)
pB.setTitleColor(UIColor .green, for: .normal)
pB.translatesAutoresizingMaskIntoConstraints = false
pB.addTarget(self, action: #selector(postingTextPost), for: .touchUpInside)
return pB
}()


func postingTextPost(){

...

}

func setupTextWindow(){

let window: UIWindow = UIApplication.shared.keyWindow!

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = window.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
window.addSubview(blurEffectView)

window.addSubview(PostBarContainer)
window.addSubview(PostBar)
window.addSubview(PostButton)

PostBarContainer.centerXAnchor.constraint(equalTo: window.centerXAnchor).isActive = true
PostBarContainer.bottomAnchor.constraint(equalTo: window.centerYAnchor).isActive = true
PostBarContainer.widthAnchor.constraint(equalTo: window.widthAnchor).isActive = true
PostBarContainer.heightAnchor.constraint(equalToConstant: 50).isActive = true

PostBar.bottomAnchor.constraint(equalTo: PostBarContainer.bottomAnchor).isActive = true
PostBar.widthAnchor.constraint(equalTo: PostBarContainer.widthAnchor, constant: -80).isActive = true
PostBar.heightAnchor.constraint(equalTo: PostBarContainer.heightAnchor).isActive = true

PostButton.bottomAnchor.constraint(equalTo: PostBarContainer.bottomAnchor).isActive = true
PostButton.widthAnchor.constraint(equalToConstant: 90).isActive = true
PostButton.rightAnchor.constraint(equalTo: PostBar.rightAnchor, constant: 80).isActive = true
PostButton.heightAnchor.constraint(equalTo: PostBarContainer.heightAnchor).isActive = true

}

我在 UIViewController 中有一些代码来执行 post 函数。但是,在 UIWindow 中,我无法将该函数称为“它可能是有史以来最小的问题”。该函数也在viewcontroller 内部。它与函数无关,只是没有通过 PostButton 上的 #selector 调用。

我知道我在这里缺少一些东西,但是就在键窗口内执行功能而言,最佳实践是什么?我知道无论如何都不应该把所有东西都扔到 View Controller 中,但是,是的,对于一些建议/提示也非常有用! <3

最佳答案

按钮的操作是否未被识别,或者只是没有调用正确的函数?

如果客人没有被识别,那么我相信这与父 View 是 UIViewController 而不是 UIWindow 有关。您正在初始化按钮并添加访客识别器,将父级设置为“self”。在这种情况下,Self 将是 UIViewController,而实际上父 View 需要是 UIWindow。我建议将窗口更改为类级别常量,然后将您的访客识别行更改为

pB.addTarget(window, action: #selector(postingTextPost), for: .touchUpInside)

关于ios - Swift 3 无法使用 UIWindow 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45641137/

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