1

I have written a class where a view is displayed that has a UITextView and a couple of buttons. Everything works except the buttons don't respond to a tap. Below is what I think is the relevant code in my class:

class FullPrompt:UIViewController {
    var canceledPressedCallback:(()->()?)?=nil
    var okPressedCallback:((_ txt: String)->()?)? = nil
    var popupView = UIView()
    var field = UITextView()
    var parentView = UIView()

    func prompt(_ message:String,view: UIViewController,numberInput: Bool, callback: @escaping (_ txt: String)->()) {
        prompt(message, view: view, numberInput: numberInput, callback: callback,cancelCallback: nil,defaultText: "")
    }

    func prompt(_ prompt:String,view: UIViewController,numberInput: Bool, callback: @escaping (_ txt: String)->(), cancelCallback: (()->())?,defaultText: String) {
        canceledPressedCallback=cancelCallback
        okPressedCallback=callback

        let cancelButton=UIButton(frame: CGRect(x: 10, y: y, width: 70, height: 32))
        cancelButton.setTitle("Cancel", for: .normal)
        cancelButton.addTarget(self, action: #selector(cancelButtonPressed(_:)), for: .touchUpInside)
        popupView.addSubview(cancelButton)
        let okButton=UIButton(frame: CGRect(x: 210, y: y, width: 70, height: 32))
        okButton.setTitle("Ok", for: .normal)
        okButton.setTitleColor(UIColor.blue, for: .normal)
        okButton.addTarget(self, action: #selector(okButtonPressed(_:)), for: .touchUpInside)
        popupView.addSubview(okButton)
        view.view.addSubview(popupView)
    }

    func cancelButtonPressed(_ sender: UIButton ) {
        popupView.removeFromSuperview()
        if canceledPressedCallback != nil {
            canceledPressedCallback!()
        }
    }

    func okButtonPressed(_ sender: UIButton) {
        popupView.removeFromSuperview()
        okPressedCallback!(field.text)
    }
}

What am I doing wrong?

3
  • None of your views have useful frames. Commented Nov 13, 2017 at 23:00
  • Not sure how you instantiate and display this VC, but you seem to be adding buttons from this VC to the calling VC's view hierarchy. If doing this, you must invoke certain methods so that both VCs know what's going on (otherwise, for example, touches might not be recognised). See this answer for more detail. Commented Nov 13, 2017 at 23:41
  • Strangely in my case, the buttons inside the containerView of scrollview went completely unresponsive. The issue was the containerView didn't have the Height constraint or fixed height, once added everything was back to normal. Commented Aug 25, 2022 at 8:22

1 Answer 1

1

I got it figured out. My mistake was adding a view property (popupView) within my class. Since my class was a subclass of UIViewController, I needed to add my subviews to the view property of the class, not create another view. So, basically, wherever I add adding something to popupView, I just replaced it with self.view. They the buttons worked fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.