ios objective-c delegates protocols presentmodalviewcontroller

ios - Actualización de datos en un UIViewController después de descartar su controlador de vista modal presentado a través de un delegado



objective-c delegates (2)

Deberías volver a cargar tu UI después de configurar el "cliente"

- (void)passCustomer:(SCCustomer *)customer { self.customer = customer; //At this point, self.customer has the correct reference [self dismissViewControllerAnimated:YES completion:^{ // reload your UI here or call a method which will reload your ui // e.g. for tableView it will be [tableView reload]; }]; }

Tengo el delegado trabajando mientras se pasan los datos del controlador de vista modal al controlador de vista actual. Pero el controlador de vista que presenta no muestra los datos que recibe del modal. Miré otras publicaciones y me dijeron que utilizara el método delegado / protocolo, pero no explico cómo / por qué se actualiza el VC que lo presenta. Supongo que mi delegado está configurado incorrectamente. De lo contrario, ¿cuál es el método para actualizar los datos? Lo he comprobado y no se llama a viewWillAppear y viewDidAppear.

SCCustomerDetailVC.h (Presentación de VC)

#import "SCCustomersVC.h" @interface SCCustomerDetailVC : UIViewController <SCCustomersVCDelegate> @property (atomic, strong) SCCustomer *customer; @property (strong, nonatomic) IBOutlet UIButton *changeCustomerButton; - (IBAction)changeCustomerButtonPress:(UIButton *)sender; @end

SCCustomerDetailVC.m (presentación de VC)

- (IBAction)changeCustomerButtonPress:(UIButton *)sender { UINavigationController *customersNC = [self.storyboard instantiateViewControllerWithIdentifier:@"customersNC"]; SCCustomersVC *customersVC = (SCCustomersVC *)customersNC.topViewController; customersVC.delegate = self; [self presentViewController:customersNC animated:YES completion:nil]; } //Protocol methods - (void)passCustomer:(SCCustomer *)customer { self.customer = customer; //At this point, self.customer has the correct reference [self dismissViewControllerAnimated:YES completion:nil]; }

SCCustomersVC.h (VC modal)

#import "SCCustomersVCDelegate.h" @class SCCustomerDetailVC; @interface SCCustomersVC : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> @property (weak, nonatomic) id <SCCustomersVCDelegate> delegate; @end

SCCustomersVC.m (VC modal)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { SCCustomer *customer = [self customerAtIndexPath:indexPath]; [self.delegate passCustomer:customer]; }

SCCustomersVCDelegate.h

@class SCCustomer; @protocol SCCustomersVCDelegate <NSObject> @optional - (void)passCustomer:(SCCustomer *)customer; @end


Creo que estás cerca. EDITAR - Acabo de enterarme aquí que el comportamiento de viewWillAppear es diferente en iOS> 5. Aún desea que aparezca la vista para actualizar su vista con el estado de su modelo, ya que necesita hacer eso en la presentación inicial.

Y está bien llamarlo desde su vc modal o desde el método delegado. Así que agregue código para verWillAppear que actualiza la vista con el estado del modelo del controlador de vista ...

// SCCustomerDetailVC.m - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // making up an IBOutlet called someLabel // making up a model method (description) that returns a string representing your model self.someLabel.text = [self.customer description]; }

Luego, ya sea desde el vc presentado o el delegado, llame a viewViewWillAppear:

- (void)passCustomer:(SCCustomer *)customer { self.customer = customer; [self viewWillAppear:YES]; [self dismissViewControllerAnimated:YES completion:^{}]; }