简体   繁体   English

Swift - 在子视图中将目标添加到 UIButton

[英]Swift - addTarget to UIButton inside a Subview

I have a problem with my UIButton .我的UIButton有问题。 Just for basic understanding:只是为了基本的理解:

  1. User taps on a button -> popUpView appears ( UIView )用户点击按钮 - > popUpView出现( UIView
  2. User taps on UIButton which is a SubView of popUpView -> popUpView dismisses用户点击UIButton这是 popUpView 的popUpView -> popUpView关闭

That is my code for that:那是我的代码:

    @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()
}

Problem:问题:

whishButton is not clickable and I have no idea why.. I'm stuck on this for a while now so I am grateful for any help, thanks:) whishButton不可点击,我不知道为什么.. 我已经坚持了一段时间,所以我很感激任何帮助,谢谢:)

UPDATE更新

This is my View Hierarchy:这是我的视图层次结构:

在此处输入图像描述

Really weird, because the selected UIImage should actually be the UIButton which is in my case behind it (100x100 square).真的很奇怪,因为选择的UIImage实际上应该是UIButton在我的情况下它后面(100x100 正方形)。 I definitely declared my whishButton as an actual UIButton()我肯定将我的whishButton声明为实际的UIButton()

first, try whit this line首先,试试这条线

self.popUpView.bringSubviewToFront(whishButton)

and if its necessary try this line如果有必要试试这条线

self.popUpView.sendSubview(yourImageBackGround)

Try to move buttons in storyboard尝试移动 storyboard 中的按钮在此处输入图像描述

SOLVED解决了

I solved my problem.. not in a very smooth way but it's working now.我解决了我的问题..不是很顺利,但它现在正在工作。 I just added the button as another subview of my normal UIView instead of adding it within my popUpView .我只是将button添加为我的普通UIView的另一个subview ,而不是在我的popUpView中添加它。

I guess, you don't create any instantiate of your root class globally.我想,您不会在全球范围内创建根 class 的任何实例。

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()
    }
}

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

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