Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Detect Touch Anywhere

I'm writing my first app, using Swift, and I need a popover or modal view that can be dismissed by touching anywhere on the screen.

I'm writing an IOU app and am currently working on the view where the user enters the lenders and how much they lend. Obviously each lender needs to have a unique name, and I'd like a popover or modal view to appear whenever the user tries to enter the same name twice asking them to change the name. To lessen the irritation factor, I'd like to make it so that the user can tap anywhere on the screen to dismiss the warning, rather than on a specific button.

I found this answer: Detect touch globally, and I think it might work for me, but I know nothing of Objective-C, just Swift, and couldn't understand enough to know what to do.

like image 208
7OO Tnega Terces Avatar asked Nov 30 '22 10:11

7OO Tnega Terces


1 Answers

Dismissing a modal view turns out to be surprisingly easy. All you need to do is call dismissViewControllerAnimated(true, completion: nil). Thus, to do what I wanted, all I needed to do was this:

override func touchesEnded(touches: NSSet, withEvent event: UIEvent)
{
  dismissViewControllerAnimated(true, completion: nil)
  super.touchesEnded(touches, withEvent: event)
}
like image 130
7OO Tnega Terces Avatar answered Dec 10 '22 23:12

7OO Tnega Terces