uitabbarcontroller - Objective C: How to reload a view controller's table view when tab is selected -
i need reload data in view controller when it's tabbar clicked.
i using uitabbarcontrollerdelegate method below:
- (void)tabbarcontroller:(uitabbarcontroller *)tabbarcontroller didselectviewcontroller:(uiviewcontroller *)viewcontroller { if (tabbarcontroller.selectedindex == 3) { [(someviewcontroller *)viewcontroller getdata]; } }
where 'getdata' instance method in someviewcontroller class. when run app, following error
2011-07-01 02:12:11.193 onethingaday[19169:207] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[uinavigationcontroller getdata]: unrecognized selector sent instance 0x600d500'
can advise me how can overcome issue? need trigger 'getdata' method when tabbarcontroller.selected index ==3
it seems me error message get, use uinavigationcontroller
in tab controller; in case, cannot send directly getdata
message it; should first find out view controller under uinavigationcontroller
should receive message. (this not related tab bar selectedindex
)
i don't know how uinavigationcontroller organized, do:
- (void)tabbarcontroller:(uitabbarcontroller *)tabbarcontroller didselectviewcontroller:(uiviewcontroller *)viewcontroller { if (tabbarcontroller.selectedindex == 3) { //-- option 1: getdata goes first view controller in uinavigationcontroller: [[(someviewcontroller*)[(uinavigationcontroller*)viewcontroller topviewcontroller] getdata]; //-- option 2: getdata goes last view controller in uinavigationcontroller (the visible one): [[(someviewcontroller*)[(uinavigationcontroller*)viewcontroller visibleviewcontroller] getdata]; } }
if give more details organization of uinavigationcontroller
can further identifying right option.
anyway, can see casts, there not ok design. suggest using notification that. i.e., someviewcontroller
registers notification of given type :
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(someselector:) name:shouldgetdatanotification object:nil];
and tab bar controller sends notification controller react upon:
- (void)tabbarcontroller:(uitabbarcontroller *)tabbarcontroller didselectviewcontroller:(uiviewcontroller *)viewcontroller { if (tabbarcontroller.selectedindex == 3) { [[nsnotificationcenter defaultcenter] postnotificationname:shouldgetdatanotification object:nil]; } .... }
look @ this post.
Comments
Post a Comment