ios - UIButton 不在 UIWindow 的 subview 上工作

标签 ios objective-c swift uibutton

我正在尝试在 Swift 中创建一个自定义的一键式 AlertViewController,我使用 window.addSubview() 来显示 AlertView,但是当触摸 AlertView 上的按钮时,func buttonTapped() 不起作用,下面是我的代码,请告诉我这里有什么问题,谢谢。

MyAlertViewController.swift

class MyAlertViewController: UIViewController {
    var button: UIButton!
    var contentView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        setUp()
    }

    func setUp(){
        contentView = UIView(frame: CGRectMake(0,0,200,300))
        contentView.center = view.center
        contentView.backgroundColor = UIColor.whiteColor()
        contentView.layer.cornerRadius = 10

        button = UIButton(type: .System)
        button.frame = CGRectMake(50, 150, 100, 40)
        button.setTitle("button", forState: UIControlState.Normal)
        button.addTarget(self, action: #selector(buttonTapped(_:)), forControlEvents: UIControlEvents.TouchUpInside)

        view.addSubview(contentView)
        contentView.addSubview(button)
        view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1)
        view.userInteractionEnabled = true
    }

    func buttonTapped(sender: AnyObject?){
        print("button tapped")
    }

    func show(){
        let window = UIApplication.sharedApplication().keyWindow! as UIWindow
        view.frame = window.bounds
        window.addSubview(view)
    }
}

ParentViewController.swift(是我窗口的rootViewController)

class ParentViewController: UIViewController {
    var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        button = UIButton(type: .System)
        button.frame = CGRectMake(110,269,100,30)
        button.setTitle("show", forState: .Normal)
        button.addTarget(self, action: #selector(buttonTapped(_:)), forControlEvents: .TouchUpInside)
        view.addSubview(button)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func buttonTapped(sender: AnyObject?){
        MyAlertViewController().show()
    }

}

enter image description here

我发现如果我如下更改 ParentViewController.swift,警报 View 上的按钮可以正常工作。

class ParentViewController: UIViewController {
    var button: UIButton!
    var vc: MyAlertViewController!

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.cyanColor()
        button = UIButton(type: .System)
        button.frame = CGRectMake(110,269,100,30)
        button.setTitle("show", forState: .Normal)
        button.addTarget(self, action: #selector(buttonTapped(_:)), forControlEvents: .TouchUpInside)
        view.addSubview(button)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func buttonTapped(sender: AnyObject?){
        vc = MyAlertViewController()
        vc.show()
    }

}

但我打算在每次用户收到新消息时显示此警报 View ,因此它会随时显示在任何 ViewController 上,所以我不想在每个 ViewController 中声明它,所以我该如何解决这个?

最佳答案

TLDR

您必须保留 MyAlertViewController() 的实例。

对此进行更改,它将起作用(正如您已经完成的那样):

// keep some reference
var alert: MyAlertViewController?

func buttonTapped(sender: AnyObject?){
    alert = MyAlertViewController()
    alert?.show()
}

更多解释

在 MyAlertViewController 中调用的 button.addTarget(self, ...) 不保留 self

addTarget 函数文档的最后一行说:

// ... the action cannot be NULL. Note that the target is not retained.*

所以在离开这个函数后将没有self来发送 Action :

func buttonTapped(sender: AnyObject?){
    MyAlertViewController().show()
}

另一种选择, 是在 MyAlertViewController 中保留 self 变量:

// retain self manually
var mySelf: MyAlertViewController?

func setUp(){
    ...
    // reference to self
    mySelf = self
    button.addTarget(mySelf, action: #selector(buttonTapped(_:)), forControlEvents: UIControlEvents.TouchUpInside)
}

关于ios - UIButton 不在 UIWindow 的 subview 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38795117/

相关文章:

ios - 自定义 UITableViewCell,单元格中的按钮不起作用

ios - 使用 MKPinAnnotationView 和 MKAnnotationView 更新用户位置

ios - 不同 View 的不同 UINavigationBar 背景

ios - 删除与 iOS 中的模式匹配的文件

ios - UITableView 部分标题故障?

ios - NSURLSession 内部有 NSoperationqueue 吗?

ios - 如何在不同的 UIViewController 之间传递数据?

objective-c - OCMock: stub @dynamic 属性

swift - 如何处理多种日期格式?

ios - 我如何创建一个 iOS CAAnimation 并自己定义每个插值?