ios - Update parent view from child view -
i using xcode 5 (ios sdk 7.0). trying update element inside parent view child view.
i have following files: viewonecontroller viewtwocontroller
inside viewonecontroller, adding viewtwocontroller subview following code:
[self.view addsubview:viewtwocontroller];
i have uiimageview declated in viewonecontroller.h
iboutlet uiimageview *box;
i update backgound color of box viewtwocontroller. know can update background color following code.
[box setbackgroundcolor:[uicolor redcolor]];
but works in viewonecontroller. have function called updatecolor viewtwocontroller. want update color of box function.
one quick , not clean way call parentviewcontroller directly :
[((viewonecontroller*)self.parentviewcontroller).box setbackgroundcolor:[uicolor redcolor]];
however larme suggested it, believe cleanest way use delegate, used notify change after user did something. here tutorial http://www.tutorialspoint.com/ios/ios_delegates.htm
you have create delegate protocol in viewtwocontroller.h
@protocol viewtwodelegate <nsobject> - (void)somethinghappenedinviewtwocontroller; @end @interface viewtwocontroller : uiviewcontroller .... @property (nonatomic,weak) id<viewtwodelegate> delegate;
in viewonecontroller have set controller delegate viewtwocontroller. in viewonecontroller.h first declare controller implement viewtwodelegate protocol
@interface viewonecontroller:uiviewcontroller<viewtwodelegate>
and in viewonecontroller.m when create viewtwocontroller :
viewtwocontroller = [[viewtwocontroller alloc] init]; viewtwocontroller.delegate = self;
and add following method below
- (void)somethinghappenedinviewtwocontroller{ [box setbackgroundcolor:[uicolor redcolor]]; }
then in viewtwocontroller.m call method, call method implemented in viewonecontroller
[self.delegate somethinghappenedinviewtwocontroller];
Comments
Post a Comment