objective c - Can't get custom @protocol working on iOS -
note: below using ios automatic reference counting (arc) enabled. think arc may have lot why isn't working set per examples i've found via google.
i trying create protocol notify delegate of filename user selects uitableview.
filelistviewcontroller.h
@protocol filelistdelegate <nsobject> - (void)didselectfilename:(nsstring *)filename; @end @interface filelistviewcontroller : uitableviewcontroller { @private nsarray *filelist; id <filelistdelegate> delegate; } @property (nonatomic, retain) nsarray *filelist; @property (nonatomic, assign) id <filelistdelegate> delegate; @end
filelistviewcontroller.m
#import "filelistviewcontroller.h" @implementation filelistviewcontroller @synthesize filelist; @synthesize delegate;
this gives error @
@synthesize delegate;
line "filelistviewcontroller.m: error: automatic reference counting issue: existing ivar 'delegate' unsafe_unretained property 'delegate' must __unsafe_unretained"
if change filelistviewcontroller.h putting __weak , (weak) run.
@protocol filelistdelegate <nsobject> - (void)didselectfilename:(nsstring *)filename; @end @interface filelistviewcontroller : uitableviewcontroller { @private nsarray *filelist; __weak id <filelistdelegate> delegate; } @property (nonatomic, retain) nsarray *filelist; @property (weak) id <filelistdelegate> delegate; @end
but when try set delegate app crashes. view called 'importviewcontroller' creating view 'filelistviewcontroller' , setting delegate (importviewcontroller) can implement custom protocol of 'didselectfilename'. error is;
* terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[importviewcontroller setdelegate:]: unrecognized selector sent instance 0x6c7d430'
the code running is;
importviewcontroller.m
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { filelistviewcontroller *filelistviewcontroller = [self.storyboard instantiateviewcontrollerwithidentifier:@"filelist"]; [filelistviewcontroller setdelegate:self]; [self.navigationcontroller pushviewcontroller:filelistviewcontroller animated:yes]; }
my questions are:
- why putting (weak) , __weak in make work? don't understand why works found googling , there wasn't explanation.
- why can't set delegate using '[filelistviewcontroller setdelegate:self];' ? seems compiler doesn't know 'delegate' exists.
it seems :
filelistviewcontroller *filelistviewcontroller = [self.storyboard instantiateviewcontrollerwithidentifier:@"filelist"];
you didn't filelistviewcontroller
object. @ message says :
-[importviewcontroller setdelegate:]: unrecognized selector sent instance 0x6c7d430
and why app crashes. try define retain property, instead of assign, in case delegate deallocated elsewhere, app won't crash.
Comments
Post a Comment