ios - UIButtonTypeDetailDisclosure : unrecognised selector sent to instance -
ios - UIButtonTypeDetailDisclosure : unrecognised selector sent to instance -
i have custom button class:
custombutton.h file:
@interface custombutton : uibutton @property (nonatomic, retain) nsstring* info; @end custombutton.m file:
#import "custombutton.h" @implementation custombutton @synthesize info; - (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { // initialization code } homecoming self; } @end in main view controller:
custombutton* btn = [custombutton buttonwithtype:uibuttontypedetaildisclosure]; [btn setinfo:@"foobar"]; nslog(@"%@", [btn info]); [self.view addsubview:btn]; if it's simple button ([custombutton new]) don't error. if take buttonwithtype:uibuttontypedetaildisclosure error:
*** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[uibutton setinfo:]: unrecognized selector sent instance 0x753c8c0' why happening?
as uibutton not provide initwithtype: method - can't subclass "typed" button. can't create extension library class either. way "attach" predefined object utilize associated objects:
#import <objc/runtime.h> uibutton* btn = [uibutton buttonwithtype:uibuttontypedetaildisclosure]; nsstring* info = @"foobar"; objc_setassociatedobject(btn, "info", info, objc_association_retain); //later somewhere nsstring* btninfo = (nsstring*)objc_getassociatedobject(btn, "info"); the "info" can string like, it's key retrieve object later. objc_association_retain means object retained , automatically released after btn object's dealloc: called. can find more details here.
the other way solve issue subclass uibutton, add together info property , create disclosure button setting custom image setimage:forstate: method.
generally, coupling info standard ui controls sign of bad architecture. maybe you'll create little step backwards , seek find other way pass string wherether need utilize it?
ios cocoa-touch uibutton unrecognized-selector
Comments
Post a Comment