0

I'm trying to present a viewcontroller modally over current context when I press a button ("Touch Down") and when I release that button ("Touch Up Inside") I want the viewcontroller to be dismissed.

This is my code: (buttonPressed is the "Touch Down" event and buttonReleased is the "Touch Up Inside" event)

@IBAction func buttonPressed(_ sender: AnyObject) {

    let storyboard = UIStoryboard.init(name: "Main", bundle: .main)
    let anotherView = storyboard.instantiateViewController(withIdentifier: "AnotherView")
    anotherView.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
    anotherView.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
    present(anotherView, animated: true, completion: nil)

}


@IBAction func buttonReleased(_ sender: AnyObject) {
    dismiss(animated: true, completion: nil)
}

But if I spam the button anotherView gets stuck and leaves me there. Does anyone know a solution to this problem? Thank you.

EDIT: also, if I change the animation to false this still happens so it's probably not the issue.

EDIT 2 I solved the problem. I was simply listening to the wrong event from the button. If you press and release the button really quickly it seems that the event that gets fired is "Touch Cancel". I added that as well and my code is now working as it should.

nullforlife
  • 1,354
  • 2
  • 18
  • 30
  • What you mean by spam the button. – Sachin Vas Oct 19 '16 at 15:07
  • Press it very frequently – nullforlife Oct 19 '16 at 15:08
  • You can disable the button in `viewWillDisappear` and enable it in `viewDidAppear` if you don't need that frequent events to be handled – alexburtnik Oct 19 '16 at 15:10
  • @alexburtnik it was a nice idea but it did unfortunately not solve the problem. I also noticed the problem comes when I "flick" the button really quickly. – nullforlife Oct 19 '16 at 15:15
  • @nullforlife That's weird. Are you sure you didn't messed `viewWillDisappear` with `viewDidDisappear` or something like that? – alexburtnik Oct 19 '16 at 15:20
  • @alexburtnik yes, but I also just noticed they don't even get called. Maybe they don't get called when you present a viewcontroller over current context? – nullforlife Oct 19 '16 at 15:24
  • Why are you creating a storyboard manually? Why don't you just call `self.storyboard`? – Guy Kogus Oct 19 '16 at 15:26
  • @nullforlife No, they should be called when you present modally. Perhaps you're overriding those methods in a subclass and not calling super? Can't imagine another reason for lifecycle methods not being called :) – alexburtnik Oct 19 '16 at 15:30
  • @GuyKogus good question. I changed that now. – nullforlife Oct 19 '16 at 15:34
  • @alexburtnik Are you talking about anotherView's viewWillDisappear and viewWillAppear now or the 'initial' view controller? – nullforlife Oct 19 '16 at 15:35
  • @nullforlife Initial, since the button you need to disable is in your initial controller. Btw, anotherView is in fact a controller, don't mix them up ;) – alexburtnik Oct 19 '16 at 15:38
  • @alexburtnik yes exactly, that's what I thought. Just wanted to be sure. They are not called anyway though... – nullforlife Oct 19 '16 at 15:54
  • @alexburtnik http://stackoverflow.com/questions/30474034/view-controller-lifecycle-on-uimodalpresentationovercurrentcontext – nullforlife Oct 19 '16 at 15:57

0 Answers0