Swift - 将目标添加到子视图内的 UIButton

Posted

技术标签:

【中文标题】Swift - 将目标添加到子视图内的 UIButton【英文标题】:Swift - addTarget to UIButton inside a Subview 【发布时间】:2019-11-22 15:53:42 【问题描述】:

我的UIButton 有问题。只是为了基本的理解:

    用户点击按钮 -> 出现popUpView (UIView) 用户点击UIButton 这是popUpView 的子视图 -> popUpView 关闭

这是我的代码:

    @objc func addWishButtonTapped(notification : Notification)
    
    view.addSubview(popUpView)
    
    popUpView.addSubview(popUpTextField)
    popUpView.addSubview(wishButton)
    
    
    // constrain popUpView
    popUpView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    popUpView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -100).isActive = true
    popUpView.heightAnchor.constraint(equalToConstant: 200).isActive = true
    popUpView.widthAnchor.constraint(equalToConstant: view.frame.width - 85).isActive = true
    
    // constrain wishButton
    wishButton.centerXAnchor.constraint(equalTo: popUpView.centerXAnchor).isActive = true
    wishButton.centerYAnchor.constraint(equalTo: popUpView.centerYAnchor, constant: 130).isActive = true
    wishButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
    wishButton.widthAnchor.constraint(equalToConstant: 100).isActive = true

    // constrain textField
    popUpTextField.centerXAnchor.constraint(equalTo: popUpView.centerXAnchor).isActive = true
    popUpTextField.centerYAnchor.constraint(equalTo: popUpView.centerYAnchor, constant: -50).isActive = true
    popUpTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true
    popUpTextField.widthAnchor.constraint(equalToConstant: view.frame.width - 170).isActive = true

    popUpView.transform =  CGAffineTransform(scaleX: 1.3, y: 1.3)
    popUpView.alpha = 0
    
    UIView.animate(withDuration: 0.3) 
        self.visualEffectView.alpha = 1
        self.popUpView.alpha = 1
        self.popUpView.transform = CGAffineTransform.identity
    
    
    // make whishButton clickable
    self.wishButton.addTarget(self, action: #selector(wishButtonTapped), for: .touchUpInside)


@objc func wishButtonTapped()
    print("test")
    insertWhish()
    dismissPopUpView()

问题:

whishButton 不可点击,我不知道为什么.. 我在这个问题上停留了一段时间,所以我很感激任何帮助,谢谢:)

更新

这是我的视图层次结构:

真的很奇怪,因为选择的UIImage 实际上应该是UIButton,在我的情况下它在它后面(100x100 正方形)。我肯定将我的whishButton 声明为实际的UIButton()

【问题讨论】:

你检查视图的层次结构吗? @Sammy 不工作:/ @AndresGomez 你到底是什么意思?:) @DieGlueckswurst Xcode 可以向您展示此层次结构,查看这篇文章 (developer.apple.com/library/archive/documentation/…) 并告诉我您的按钮是否位于某个视图后面。 @AndresGomez 我实际上从未使用过调试器,对不起,我对此很陌生,但我会尝试 :) 【参考方案1】:

首先,试试这条线

self.popUpView.bringSubviewToFront(whishButton)

如果有必要试试这一行

self.popUpView.sendSubview(yourImageBackGround)

【讨论】:

不工作。我发现,我故意将Button 放在我的popUpView 下方(在它外面)。如果我把它放在里面,按钮是可点击的。知道为什么会发生这种情况以及如何解决吗?【参考方案2】:

尝试在情节提要中移动按钮

【讨论】:

我以编程方式做所有事情【参考方案3】:

已解决

我解决了我的问题.. 不是很顺利,但它现在可以工作了。我只是将button 添加为我普通UIView 的另一个subview,而不是在我的popUpView 中添加它。

【讨论】:

【参考方案4】:

我猜,您不会在全局范围内创建任何根类的实例化。

class ViewController: UIViewController 
    var viewController: MyViewController
    override func viewDidLoad() 
        super.viewDidLoad()
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        myViewController =storyBoard.instantiateViewController(withIdentifier:"MyViewController") as! MyViewController
        self.present(myViewController, animated:true, completion:nil)
        myViewController.addWishButtonTapped(nil)
    



class MyViewController: UIViewController 
    let popUpView = UIView()
    let popUpTextField = UITextField()
    let wishButton = UIButton()

    override func viewDidLoad() 
        super.viewDidLoad()
    

    @objc func addWishButtonTapped(notification : Notification?)
        view.addSubview(popUpView)

        popUpView.addSubview(popUpTextField)
        popUpView.addSubview(wishButton)


        // constrain popUpView
        popUpView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        popUpView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -100).isActive = true
        popUpView.heightAnchor.constraint(equalToConstant: 200).isActive = true
        popUpView.widthAnchor.constraint(equalToConstant: view.frame.width - 85).isActive = true

        // constrain wishButton
        wishButton.centerXAnchor.constraint(equalTo: popUpView.centerXAnchor).isActive = true
        wishButton.centerYAnchor.constraint(equalTo: popUpView.centerYAnchor, constant: 130).isActive = true
        wishButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
        wishButton.widthAnchor.constraint(equalToConstant: 100).isActive = true

        // constrain textField
        popUpTextField.centerXAnchor.constraint(equalTo: popUpView.centerXAnchor).isActive = true
        popUpTextField.centerYAnchor.constraint(equalTo: popUpView.centerYAnchor, constant: -50).isActive = true
        popUpTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true
        popUpTextField.widthAnchor.constraint(equalToConstant: view.frame.width - 170).isActive = true

        popUpView.transform =  CGAffineTransform(scaleX: 1.3, y: 1.3)
        popUpView.alpha = 0

        UIView.animate(withDuration: 0.3) 
            self.visualEffectView.alpha = 1
            self.popUpView.alpha = 1
            self.popUpView.transform = CGAffineTransform.identity
        

        // make whishButton clickable
        self.wishButton.addTarget(self, action: #selector(wishButtonTapped), for: .touchUpInside)
    

    @objc func wishButtonTapped()
        print("test")
        insertWhish()
        dismissPopUpView()
    

【讨论】:

以上是关于Swift - 将目标添加到子视图内的 UIButton的主要内容,如果未能解决你的问题,请参考以下文章

将 UILabel 添加到子视图后如何重写它?

Swift 3:将数据从 TabBarController 传递到子视图控制器

将视图添加到子视图

如何通过调用函数将数据从视图传递到子视图?

从另一个方法将子视图添加到子视图

将 UIButton 添加到子视图不起作用