ios - Update Image from Secondary View Controller -


i need update image on main view controller pop view controller.

the button called 'feature2btn' on main view (eraviewcontroller) when try following code on popup view controller won't work.

it needs immediate update main view still showing in background , not reload change needs directly caused action on pop view.

- (ibaction)purchase:(id)sender {  homecontroller = [[eraviewcontroller alloc] initwithnibname:@"eraviewcontroller" bundle:nil];     uiimage *image = [uiimage imagenamed:@"ico_plan.png"];     [(eraviewcontroller*)homecontroller setfeature2btn:[feature2btn setimage:[uiimage imagenamed:@"image.png"] forstate:uicontrolstatenormal];  } 

there (at least) 2 ways this:

  1. you use notification 1 controller listens , other sends @ appropriate time.
  2. you create delegate protocol first controller implements , second on calls.

the delegate 1 bit more complicated considered style. notification 1 not bad, either, less "elegant". describe notification based 1 here, because seems ok case , allow react purchase in multiple places registering notification there, too.

in controller has image updated, register notification in viewdidappear::

[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(updateimage:) name:@"updateimagenotification" object:nil]; 

implement updateimage: method:

-(void)updateimage:(nsnotification*)note {     nsstring* newimagename = note.userinfo[@"imagefilekey"];     // ... update ui new image } 

also make sure deregister notification when view goes away:

-(void)viewwilldisappear:(bool)animated {      [[nsnotificationcenter defaultcenter] removeobserver:self];      [super viewwilldisappear:animated]; } 

in other controller, triggers update, fire notification @ appropriate place:

-(ibaction)purchase:(id)sender {     // ...      nsdictionary* userinfo = @{@"imagefilekey" : newimagename};     [[nsnotificationcenter defaultcenter]                 postnotificationname:@"updateimagenotification"                               object:self userinfo:userinfo];     // ... } 

the object parameter in notification context used specify if want listen notifications any object or specific instance. in many cases actual instance not relevant, discern notifications name (like "updateimagenotification" in case).

the userinfo dictionary intended carry along information need provide notification. that's why introduced key "imagefilekey" associated new image name.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -