Friday, 15 April 2011

ios - Crash when calling setRightBarButtonItems:animated: multiple times, why? -



ios - Crash when calling setRightBarButtonItems:animated: multiple times, why? -

i have 2 methods toggle right bar button items in view controller's uinavigationitem. it's show search bar in navigation bar when search button tapped, , hide search bar when user executes search

- (void)displaysearchui { [self.mainnavigationcontroller.topviewcontroller.navigationitem setrightbarbuttonitems:[self searchopenitems] animated:yes]; [self.searchbar becomefirstresponder]; } - (void)dismisssearchui { [self.mainnavigationcontroller.topviewcontroller.navigationitem setrightbarbuttonitems:[self searchcloseditems] animated:yes]; } - (nsarray *)searchcloseditems { homecoming @[[self somecustomitem], [self someotheritem], [self searchbuttonitem], ]; } - (nsarray *)searchopenitems { homecoming @[[self somecustomitem], [self someotheritem], [self searchbaritem], ]; }

the item returned searchbuttonitem uses uibarbuttonitem image, while item returned searchbaritem has custom view of uisearchbar. other items in each identical in each search state.

when phone call displaysearchui, searchbar becoming first responder opens popover show search suggestions. when popover dismissed user, dismisssearchui called via popovercontrollerdiddismisspopover: delegate method. @ point, app encounters crash within uinavigationbar:

-[calayer enumerateobjectsusingblock:]: unrecognized selector sent instance 0x7de9ac80 *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[calayer enumerateobjectsusingblock:]: unrecognized selector sent instance 0x7de9ac80' *** first throw phone call stack: ( 0 corefoundation 0x056f0946 __exceptionpreprocess + 182 1 libobjc.a.dylib 0x05379a97 objc_exception_throw + 44 2 corefoundation 0x056f85c5 -[nsobject(nsobject) doesnotrecognizeselector:] + 277 3 corefoundation 0x056413e7 ___forwarding___ + 1047 4 corefoundation 0x05640fae _cf_forwarding_prep_0 + 14 5 uikit 0x03ba38aa -[uinavigationbar _setleftviews:rightviews:] + 3774 6 uikit 0x03b8f77e -[uinavigationitem updatenavigationbarbuttonsanimated:] + 186 7 uikit 0x03b901a0 -[uinavigationitem setobject:forleftrightkeypath:animated:] + 600 8 uikit 0x03b9100a -[uinavigationitem setrightbarbuttonitems:animated:] + 104 9 myapp 0x0028088b -[displaymanager dismisssearchui] + 283

i'm not sure happening here. seems happen whether or not setting items animated. happens when user taps outside popover. if programmatically dismiss popover , phone call dismisssearchui, there's no crash (i imagine due different order in animations occur, or due state differences in ui when user dismisses popover vs programmatic dismissal. i've tried work around doing this:

#pragma mark - uipopovercontrollerdelegate - (bool)popovercontrollershoulddismisspopover:(uipopovercontroller *)popovercontroller { [self dismisssearchsuggestionsui]; homecoming no; } - (void)dismisssearchsuggestionsui { [self.popovercontroller dismisspopoveranimated:yes]; [self.displaymanager dismisssearchui]; }

but exc_bad_access crash instead here, (with similar stack trace leads me believe it's same issue, different symptom due timing).

has encountered this, , there way work around without having disable manual user popover dismissals altogether?

bah, figured out right after posting. ammending dismisssearchui's method this:

- (void)dismisssearchui { [self.searchbar resignfirstresponder] [self.mainnavigationcontroller.topviewcontroller.navigationitem setrightbarbuttonitems:[self searchcloseditems] animated:yes]; }

i resolved issue. figured search bar resign own first responder when user executed search, doesn't happen automatically when user manually cancels search causes bar button item it's in removed nav bar.

ios objective-c cocoa-touch uinavigationbar uipopovercontroller

No comments:

Post a Comment