iphone - Using UIAppearance with a subclass of UIBarButtonItem is causing unrecognized selector being sent to UINavigationButton -


tl&dr;

setting custom property of subclass of uibarbuttonitem using uiappearance proxy causing "unrecognized selector" exception, beause setter being forwarded uiappearance uinavigationbutton, not bar button itself.


sdk overview

i using ios 7 beta 5 sdk xcode 5 dp5. please, don't tell me under nda, because not discuss new features or new classes in question. inform of sdk, because can found out it's bug in beta software.

what did

i subclassed uibarbuttonitem , created custom property in header file:

@property (nonatomic, strong) nsstring *mysubclassedproperty ui_appearance_selector; 

my setter , getter this:

- (void)setmysubclassedproperty:(nsstring *)mysubclassedproperty {     _mysubclassedproperty = mysubclassedproperty;     nslog(@"%p %s %@", self, __pretty_function__, mysubclassedproperty); } 

nothing special, huh? doesn't work uiappearance @ all. when try set default appearance in application's delegate, gives me no error, no warning, whatsoever.

[[akbarbuttonitem appearance] setmysubclassedproperty:@"globally assigned"]; 

the crash

it seems working charm, except of fact crashes when try set instance of akbarbuttonitem self.navigationitem.rightbarbuttonitem:

self.navigationitem.rightbarbuttonitem = [[akbarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemaction target:nil action:nil]; 

the backtrace looks this:

2013-08-13 14:30:08.551 uibarbuttonitem subclass demo[1512:a0b] -[uinavigationbutton setmysubclassedproperty:]: unrecognized selector sent instance 0x8c42710 2013-08-13 14:30:08.556 uibarbuttonitem subclass demo[1512:a0b] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[uinavigationbutton setmysubclassedproperty:]: unrecognized selector sent instance 0x8c42710' *** first throw call stack: (     0   corefoundation                      0x0173b6f4 __exceptionpreprocess + 180     1   libobjc.a.dylib                     0x014bb8b6 objc_exception_throw + 44     2   corefoundation                      0x017d8983 -[nsobject(nsobject) doesnotrecognizeselector:] + 275     3   corefoundation                      0x0172ba1b ___forwarding___ + 1019     4   corefoundation                      0x0172b5fe _cf_forwarding_prep_0 + 14     5   corefoundation                      0x0172fe2d __invoking___ + 29     6   corefoundation                      0x0172fd3a -[nsinvocation invoke] + 362     7   corefoundation                      0x0172feba -[nsinvocation invokewithtarget:] + 74     8   uikit                               0x00763255 workaround10030904invokewithtarget + 824     9   uikit                               0x0075dea8 +[_uiappearance _applyinvocationsto:window:matchingselector:] + 4497     10  uikit                               0x0075e299 +[_uiappearance _applyinvocationsto:window:] + 55     11  uikit                               0x0029ddcb -[uiview(calayerdelegate) layoutsublayersoflayer:] + 183     12  libobjc.a.dylib                     0x014cd81f -[nsobject performselector:withobject:] + 70     13  quartzcore                          0x03ac172a -[calayer layoutsublayers] + 148     14  quartzcore                          0x03ab5514 _zn2ca5layer16layout_if_neededepns_11transactione + 380     15  quartzcore                          0x03ac3b55 -[calayer(calayerprivate) layoutbelowifneeded] + 43     16  uikit                               0x00290886 -[uiview(hierarchy) layoutbelowifneeded] + 595     17  uikit                               0x0029062d -[uiview(hierarchy) layoutifneeded] + 74     18  uikit                               0x0050841e -[uibarbuttonitem(uistatic) _leftrightimagepaddingforedgemargininnavbarismini:] + 574     19  uikit                               0x002ea325 -[uinavigationbar _gettitleviewframe:leftviewframes:rightviewframes:foritematindex:returnedidealwidthoftextcontent:availablelayoutwidthfortextcontent:idealbackbuttonwidth:] + 3522     20  uikit                               0x002ef2da -[uinavigationbar _gettitleviewframe:leftviewframes:rightviewframes:foritematindex:] + 591     21  uikit                               0x002ef59a -[uinavigationbar _gettitleviewframe:leftviewframes:rightviewframes:] + 151     22  uikit                               0x002dc515 -[uinavigationbar _setleftviews:rightviews:] + 1894     23  uikit                               0x002ca6c5 -[uinavigationitem updatenavigationbarbuttonsanimated:] + 188     24  uikit                               0x002cab63 -[uinavigationitem setobject:forleftrightkeypath:animated:] + 547     25  uikit                               0x002cb1ae -[uinavigationitem setrightbarbuttonitem:animated:] + 171     26  uikit                               0x002cb0fe -[uinavigationitem setrightbarbuttonitem:] + 48     27  uibarbuttonitem subclass demo       0x0000665e -[akviewcontroller viewdidload] + 222     28  uikit                               0x00345c18 -[uiviewcontroller loadviewifrequired] + 696     29  uikit                               0x00345eb4 -[uiviewcontroller view] + 35     30  uikit                               0x00370369 -[uinavigationcontroller rotatingsnapshotviewforwindow:] + 52     31  uikit                               0x00698e10 -[uiclientrotationcontext initwithclient:toorientation:duration:andwindow:] + 420     32  uikit                               0x00276ff2 -[uiwindow _setrotatableclient:toorientation:updatestatusbar:duration:force:isrotating:] + 1495     33  uikit                               0x00276a16 -[uiwindow _setrotatableclient:toorientation:updatestatusbar:duration:force:] + 82     34  uikit                               0x002768e8 -[uiwindow _setrotatablevieworientation:updatestatusbar:duration:force:] + 117     35  uikit                               0x00276970 -[uiwindow _setrotatablevieworientation:duration:force:] + 67     36  uikit                               0x00275a0a __57-[uiwindow _updatetointerfaceorientation:duration:force:]_block_invoke + 120     37  uikit                               0x0027596c -[uiwindow _updatetointerfaceorientation:duration:force:] + 400     38  uikit                               0x002766c3 -[uiwindow setautorotates:forceupdateinterfaceorientation:] + 870     39  uikit                               0x00279c7e -[uiwindow setdelegate:] + 449     40  uikit                               0x0034a037 -[uiviewcontroller _trybecomerootviewcontrollerinwindow:] + 180     41  uikit                               0x0026f91c -[uiwindow addrootviewcontrollerviewifpossible] + 609     42  uikit                               0x00270146 -[uiwindow setrootviewcontroller:] + 960     43  uibarbuttonitem subclass demo       0x00006a07 -[akappdelegate application:didfinishlaunchingwithoptions:] + 823     44  uikit                               0x0022d525 -[uiapplication _handledelegatecallbackswithoptions:issuspended:restorestate:] + 309     45  uikit                               0x0022dd65 -[uiapplication _callinitializationdelegatesforurl:payload:suspended:] + 1536     46  uikit                               0x00232578 -[uiapplication _runwithurl:payload:launchorientation:statusbarstyle:statusbarhidden:] + 824     47  uikit                               0x0024657c -[uiapplication handleevent:withnewevent:] + 3447     48  uikit                               0x00246ae9 -[uiapplication sendevent:] + 85     49  uikit                               0x002341f5 _uiapplicationhandleevent + 736     50  graphicsservices                    0x0365a33b _purpleeventcallback + 776     51  graphicsservices                    0x03659e46 purpleeventcallback + 46     52  corefoundation                      0x016b6e95 __cfrunloop_is_calling_out_to_a_source1_perform_function__ + 53     53  corefoundation                      0x016b6bcb __cfrunloopdosource1 + 523     54  corefoundation                      0x016e18ac __cfrunlooprun + 2156     55  corefoundation                      0x016e0bf3 cfrunlooprunspecific + 467     56  corefoundation                      0x016e0a0b cfrunloopruninmode + 123     57  uikit                               0x00231cad -[uiapplication _run] + 840     58  uikit                               0x00233f0b uiapplicationmain + 1225     59  uibarbuttonitem subclass demo       0x00006e9d main + 141     60  libdyld.dylib                       0x01d77725 start + 0 ) libc++abi.dylib: terminating uncaught exception of type nsexception 

after examination strangly realized setmysubclassedproperty: message sent uinavigationbutton (which private subclass of uibutton used in navigation bars, toolbars , search bars). set button in frame 26 , uiappearance magic executed in frame 10.

in ios 6, issue exists different message sent uinavigationbutton:

2013-08-13 14:35:58.316 uibarbuttonitem subclass demo[1591:c07] -[uinavigationbutton _uiappearance_setmysubclassedproperty:]: unrecognized selector sent instance 0x810d600 2013-08-13 14:35:58.359 uibarbuttonitem subclass demo[1591:c07] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[uinavigationbutton _uiappearance_setmysubclassedproperty:]: unrecognized selector sent instance 0x810d600' *** first throw call stack: (0x12b2012 0x10d7e7e 0x133d4bd 0x12a1bbc 0x12a194e 0x12a61bd 0x12a60d6 0x12a624a 0x68b9e8 0x68769c 0x687a95 0x291884 0x3088ca 0x28cf96 0x2934a4 0x28b89d 0x2cc646 0x2bc076 0x2bc517 0x2bcb66 0x2bcab6 0x665e 0x3261c7 0x326232 0x3264da 0x33d8e5 0x33d9cb 0x33dc76 0x33dd71 0x33e89b 0x33e9b9 0x33ea45 0x44420b 0x2952dd 0x10eb6b0 0x289dfc0 0x289233c 0x289deaf 0x3342bd 0x27cb56 0x27b66f 0x27b589 0x27a7e4 0x27a61e 0x27b3d9 0x27e2d2 0x32899c 0x275574 0x275cc1 0x6a07 0x242157 0x242747 0x24394b 0x254cb5 0x255beb 0x247698 0x24b8df9 0x24b8ad0 0x1227bf5 0x1227962 0x1258bb6 0x1257f44 0x1257e1b 0x24317a 0x244ffc 0x6e9d 0x1ab0725) libc++abi.dylib: terminate called throwing exception 

deduction

my first thought was: "uiappearance forwards setter akbarbuttonitem , crashes". it? in an article peter steinberger found out apple using special subclass of _uiappearance bar items called _uibaritemappearance. wanted confirm , set symbolic breakpoint on -[_uibaritemappearance forwardinvocation:] , lldb halted before exception occurred.

now seems me apple doing dirty logic in -[_uibaritemappearance forwardinvocation:] realize selector should sent instance, like:

  • setbackgroundimage:forstate: should sent uinavigationbutton
  • setbackbuttonbackgroundimage:forstate:barmetrics: uibarbuttonitem
  • ...

and code looks this:

if (selector == @selector(setbackbuttonbackgroundimage:forstate:barmetrics:)) {     // forward uibarbuttonitem } else if (selector == @selector(setbackbuttonbackgroundverticalpositionadjustment:forbarmetrics:)) {     // forward uibarbuttonitem } else if (...) {     // more forwarding uibarbuttonitem } else {     // forward uinavigationbutton } 

then, according deduction, last else statement causing setnicebarbuttonitemattributes: selector being sent uinavigationbutton, not uibarbuttonitem. if it's true, we're doomed.


sample code

you can download sample project here: http://cl.ly/241i2a3u0a0y.


help

am doing wrong, or bug? or maybe apple did on purpose? appreciated.

the problem uibarbuttonitem not subclass of uiview, "controller" class creates private uiview subclasses. bar button items, there private appearance proxy used, instead of usual 1 (some information here).

may suggest going different route? need subclass bar button items customization? not create subclass of uinavigationbar , use trigger changes appearancewhencontainedin:?


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -