简体   繁体   中英

Swift - addTarget to UIButton inside a Subview

I have a problem with my UIButton . Just for basic understanding:

  1. User taps on a button -> popUpView appears ( UIView )
  2. User taps on UIButton which is a SubView of popUpView -> popUpView dismisses

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:)

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). I definitely declared my whishButton as an actual 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在此处输入图像描述

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 .

I guess, you don't create any instantiate of your root class globally.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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