简体   繁体   English

UIButton选择器不起作用

[英]UIButton selector is not working

I have a view that is placed as a subview of navigationController in order to fill the whole display. 我有一个视图,它作为NavigationController的子视图放置,以填充整个显示。 On this view I have a subview that has two buttons. 在此视图上,我有一个带有两个按钮的子视图。 "Remove and Done". “删除并完成”。 and then it also has a datepicker. 然后它还有一个日期选择器。 The datePicker works, however, the Remove and Done buttons are not firing the action functions. datePicker有效,但是“删除”和“完成”按钮未激活动作功能。

The buttons: 按钮:

 var setButton: UIButton = {
   var button = UIButton(type: .system)
    button.setTitle("Done", for: .normal)

    button.tintColor = .white
    button.addTarget(self, action: #selector(handleReminderSetBtn), for: .touchUpInside)

    return button
}()

var cancelButton: UIButton = {
    var button = UIButton(type: .system)
    button.setTitle("Remove", for: .normal)
    button.tintColor = .white
    button.addTarget(self, action: #selector(handleReminderCancelBtn), for: .touchUpInside)

    return button
}()

The main blackView that is in the navigationController: 在navigationController中的主要blackView:

   viewOverLay.addSubview(cardreminder1)

   viewOverLay.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height)

   viewOverLay.backgroundColor = UIColor.black.withAlphaComponent(0.3)
   self.navigationController?.view.addSubview(viewOverLay)

CardReminder1 is the UIView on which I have two buttons. CardReminder1是UIView,上面有两个按钮。

在此处输入图片说明

I reckon there is some issue with the target in the addTarget method of the two buttons. 我认为这两个按钮的addTarget方法中的目标存在一些问题。 What could be the issue? 可能是什么问题?

You shouldn't initialize setButton and cancelButton in that way. 您不应该以这种方式初始化setButtoncancelButton

Quoting the Apple documentation from Setting a Default Property Value with a Closure or Function : 引用Apple文档中的“ 使用闭包或函数设置默认属性值”

If you use a closure to initialize a property, remember that the rest of the instance has not yet been initialized at the point that the closure is executed. 如果使用闭包来初始化属性,请记住在执行闭包时实例的其余部分尚未初始化。 This means that you cannot access any other property values from within your closure, even if those properties have default values. 这意味着您无法从闭包内部访问任何其他属性值,即使这些属性具有默认值也是如此。 moreover: 此外:

You also cannot use the implicit self property, or call any of the instance's methods, hence the problem is here: 您也不能使用隐式的self属性,或调用实例的任何方法,因此问题出在这里:

addTarget(self...)

so to fix the issue you should move the buttons initialization (or move the addTarget ) after your CardReminder1 is fully initialized. 所以要解决,你应该移动的按钮初始化的问题(或移动addTarget )后您的CardReminder1完全初始化。

I just had a similar issue. 我只是有一个类似的问题。 Along with the other answer posted, making the property lazy worked in my case as well. 在发布其他答案的同时,在我的案例中也使该属性变得lazy It depends on when you first try to access that property, but in my case I only accessed it after initialization was complete, so making the property lazy worked perfectly. 这取决于您首次尝试访问该属性的时间,但是在我的情况下,我仅在初始化完成后才访问它,因此使该属性变得lazy是完美的。

For example in your case, the following might work: 例如,在您的情况下,以下方法可能会起作用:

lazy var setButton: UIButton = {
    var button = UIButton(type: .system)
    button.setTitle("Done", for: .normal)

    button.tintColor = .white
    button.addTarget(self, action: #selector(handleReminderSetBtn), for: .touchUpInside)

    return button
}()

lazy var cancelButton: UIButton = {
    var button = UIButton(type: .system)
    button.setTitle("Remove", for: .normal)
    button.tintColor = .white
    button.addTarget(self, action: #selector(handleReminderCancelBtn), for: .touchUpInside)

    return button
}()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM