ios - "'Application tried to present modally an active controller" when presenting a view controller via a Gesture Recognizer -
i'm having bizarre issue , have been trying tackle down past few hours no luck.
i have uisearchcontroller
shows when user taps uibarbuttonitem
in navigation bar. button created ib , wired ibaction, one:
@ibaction func searchbuttontapped(sender: uibarbuttonitem) { searchcontroller.active = true self.presentviewcontroller(searchcontroller, animated: true, completion: nil) }
it works perfectly. view controller shows , user can type stuff.
additionally tapping search bar button item, want give users more power using button. namely, want them able long-press , force-touch different actions within app.
after hacking , searching on while (and because uibarbuttonitem not view on itself...), found way add gesture recognizers search bar button item. added in viewdidload
method.
if let searchbarbuttonview = self.searchbarbuttonitem.valueforkey("view") as? uiview searchbarbuttonview.respondstoselector("addgesturerecognizer:") { if settings.searchcollectionmagnifierglassiconlongpressaction != .none { let longtapgr = uilongpressgesturerecognizer(target: self, action: "handlesearchbarbuttonlongpressgesture:") longtapgr.minimumpressduration = cftimeinterval(settings.searchcollectionmagnifierglassiconlongpressactiontime) longtapgr.delegate = self searchbarbuttonview.addgesturerecognizer(longtapgr) } if settings.searchcollectionmagnifierglassiconforcetouchaction != .none { let forcetouchgr = dfcontinuousforcetouchgesturerecognizer() forcetouchgr.forcetouchdelegate = self forcetouchgr.triggeringforcetouchpressure = 2.0 searchbarbuttonview.addgesturerecognizer(forcetouchgr) } }
you can ignore force-touch related question, focusing on making long tap work first.
the gesture recognizers work fine. long tap gesture recognizer, implementation handlesearchbarbuttonlongpressgesture:
:
func handlesearchbarbuttonlongpressgesture(recognizer: uilongpressgesturerecognizer) { self.performsearchcollectionmagnifierglassaction(settings.searchcollectionmagnifierglassiconlongpressaction) }
and implementation performsearchcollectionmagnifierglassaction
:
func performsearchcollectionmagnifierglassaction(action: settings.searchcollectionmagnifierglassiconaction) { let action = settings.searchcollectionmagnifierglassiconlongpressaction if action == .clearoldqueryandsearch { self.searchcontroller.searchbar.text = "" self.searchbuttontapped(self.searchbarbuttonitem) // if user tapped search bar button item on own... crashes when gets called result of gesture recognizer! } }
(i have removed other ifs here make code relevant. other ifs check other actions , such don't called).
long-pressing search bar button works fine. app crashes with:
*** terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'application tried present modally active controller
if tap search button, app not crash. can show , hide uisearchcontroller
many times wish way. can see, long pressing search button tapping button do, except clears text field before showing it. tried commenting out line empties searchbar, crash still happens. considering same exact code gets called when long tapping , simple-tapping search button, inclined think crash may be, oddly, related gesture recognizer.
i have tried dismissing searchcontroller when it's not present before clearing searchbar , presenting again. no luck.
finally, have copied , pasted code searchbuttontapped
performsearchcollectionmagnifierglassaction
. no luck there, either.
your searchbuttontapped
method being called twice: in long tap gesture handler , directly when uibarbuttonitem's view emits touch event.
try comment out searchbuttontapped
call or set cancelstouchesinview
property of longtapgr
true
.
Comments
Post a Comment