0

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.

aejhyun
  • 612
  • 1
  • 6
  • 19

2 Answers2

2

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

Nimble
  • 1,009
  • 1
  • 11
  • 11
  • thank you so much for your answer. It worked. However, I'm not understanding why it works now. Could you possibly give an explanation as to why your answer works? – aejhyun Feb 24 '16 at 07:06
  • 1
    @Jae, glad it helped. When you want to add a view controller as a child, you always need to build parent-child relationship using `addChildViewController`, take a look at video https://developer.apple.com/videos/play/wwdc2011-102/ (session 102 -Implementing UIViewController Containment). In your cace `addChildViewController` is required to allow child controller to take part in responder chain – Nimble Feb 24 '16 at 07:50
  • Will take a look at that. Thank you :) – aejhyun Feb 24 '16 at 10:19
0

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.

Abhishek729
  • 327
  • 5
  • 14