简体   繁体   中英

Add Target for UIButton Not Being Called

I posted a similar post recently and I figured out what the problem is but I don't know how to fix the problem.

In my ImageViewerViewController , my cancelButtonTapped function is not being called. But I don't think the problem, necessarily, lies in this code. Here is the code:

var image: UIImage!
var imageView: UIImageView!
var scrollView: UIScrollView!
var disableSavingImage: Bool!
var pan: UIPanGestureRecognizer!
var cancelButton: UIButton!
var cancelButtonImage: UIImage!

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.0)
    setCancelButton()

}

func setCancelButton() {

    self.cancelButton = UIButton(type: UIButtonType.RoundedRect)
    self.cancelButton.addTarget(self, action: "cancelButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
    self.cancelButton.setImage(cancelButtonImage, forState: UIControlState.Normal)
    self.cancelButton.tintColor = UIColor.whiteColor()
    self.cancelButton.frame = CGRectMake(self.view.frame.size.width - (self.cancelButtonImage.size.width * 2) - 18, 18, self.cancelButtonImage.size.width * 2, self.cancelButtonImage.size.height * 2)
    self.view.addSubview(self.cancelButton)

}

func cancelButtonTapped(sender: UIButton) {

    print("cancelButtonTapped")

}

func centerPictureFromPoint(point: CGPoint, ofSize size: CGSize, withCornerRadius radius: CGFloat) {

    UIView.animateWithDuration(0.3, animations: { () -> Void in
        self.view.backgroundColor = UIColor(colorLiteralRed: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)

        }) { (finished: Bool) -> Void in

    }

}

I think the problem lies in my CathyTaskLogMessageViewController , when I change the code from self.view.addSubview(imageViewerViewController.view) to self.presentViewController(imageViewerViewController, animated: true, completion: nil) , my add target function gets called. Here's the code:

    func defaultPictureButtonTapped(sender: UIButton) {

        let imageViewerViewController = ImageViewerViewController()
        imageViewerViewController.image = self.defaultPictureButton.imageView!.image
        imageViewerViewController.cancelButtonImage = UIImage(named: "cancelButtonImage")
        self.view.addSubview(imageViewerViewController.view)
        imageViewerViewController.centerPictureFromPoint(self.defaultPictureButton.frame.origin, ofSize: self.defaultPictureButton.frame.size, withCornerRadius: self.defaultPictureButton.layer.cornerRadius)
//        self.presentViewController(imageViewerViewController, animated: true, completion: nil)

    }

However, I don't want to present a view controller. I want to add it as a subview. Anybody have any idea to fix this? And thank you so much for your help in advance.

The issue is in how you add child view controller. You need to call addChildViewController(imageViewerViewController) before self.view.addSubview(imageViewerViewController.view)

Once what happened to me while using subviews was that the view was in front of the subview and hence the button on the subview was not getting tapped.

Try this:-

imageViewerViewController.view.layer.zPosition = 2
//You can set any value in place of 2 which is greater than zposition value of the view
self.view.addSubview(imageViewerViewController.view)

Hope this works.

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