objective c - Passing NSMutableArray from one View Controller to a function in another -
i've read on couple of questions here on passing data between 2 view controllers , seen different ways suggested of doing it. problem follows:
i have view controller (lets call "a") has searchbar in toolbar. nothing else. have view controller ("b") responsible displaying popover view when user presses search button on keyboard when searching in searchbar. works. user enters text, presses search button, popover shown. great.
now have yet display search results in popover tableview , trying pass nsmutablearray function in b argument:
in b.h:
@property (nonatomic, retain) nsmutablearray *searchresults; in b.m:
@synthesize searchresults; -(void)setsearchresults:(nsmutablearray *)resultarray{ [self.searchresults removeallobjects]; [self.searchresults addobjectsfromarray:resultarray]; } in a.h:
#import "b.h" @property(nonatomic, retain) b *viewcontrollerobjectb; in a.m:
@synthesize viewcontrollerobjectb; //the searchresultsarray passed on function -(void)communicatewithb:(nsmutablearray *)searchresultsarray{ //i initialize viewcontrollerobjectb in here nsmutablearray *temp = [[nsmutablearray alloc] initwitharray:searchresultsarray]; [viewcontrollerobjectb setsearchresults:temp]; } now works except not content of temp passed on function in b.m. i'ts empty (nil). problem.
i'm new ios appreciated.
edit: forgot mention i'm using arc.
you didn't initialize array (that's why it's nil). in alloc method should:
- (id)init { self = [super init]; if (self) { self.searchresults = [nsmutablearray arraywithcapacity:10]; } } i spotted 2 problems in code:
@synthesize searchresults; -(void)setsearchresults:(nsmutablearray *)resultarray{ //property logic retain, need release/retain when override setter if (searchresults != resultarray) { [searchresults release]; searchresults = [resultarray retain]; } } -(void)communicatewithb:(nsmutablearray *)searchresultsarray{ //i initialize viewcontrollerobjectb in here //mem. leak on temp nsmutablearray *temp = [[[nsmutablearray alloc] initwitharray:searchresultsarray] autorelease]; [viewcontrollerobjectb setsearchresults:temp]; }
Comments
Post a Comment