Swift 3 无法获取使用 UIWindow 调用的函数


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 调用。

我知道我在这里缺少一些东西,但是就按键窗内执行函数而言,最佳实践是什么?我知道无论如何都不应该扔进视图控制器,但是是的,对于一些建议/提示也很棒!<3

是按钮的操作无法识别,还是只是没有调用正确的函数?

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

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

最新更新