在通过委托解除其呈现的模态视图控制器后,刷新UIViewController中的数据

gup*_*ron 10 delegates protocols objective-c presentmodalviewcontroller ios

我有代表工作,因为数据正从模态传递到呈现视图控制器.但是呈现视图控制器没有显示它从模态接收的数据.我查看了其他帖子,他们说要使用委托/协议方法,但不解释如何/为什么呈现VC刷新.我假设我的委托设置不正确.否则,刷新数据的方法是什么?我已经检查过了,并且没有调用viewWillAppear和viewDidAppear.

SCCustomerDetailVC.h(呈现VC)

#import "SCCustomersVC.h"

@interface SCCustomerDetailVC : UIViewController <SCCustomersVCDelegate>

@property (atomic, strong) SCCustomer *customer;
@property (strong, nonatomic) IBOutlet UIButton *changeCustomerButton;

- (IBAction)changeCustomerButtonPress:(UIButton *)sender;

@end
Run Code Online (Sandbox Code Playgroud)

SCCustomerDetailVC.m(呈现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];
}
Run Code Online (Sandbox Code Playgroud)

SCCustomersVC.h(模态VC)

#import "SCCustomersVCDelegate.h"

@class SCCustomerDetailVC;

@interface SCCustomersVC : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>

@property (weak, nonatomic) id <SCCustomersVCDelegate> delegate;

@end
Run Code Online (Sandbox Code Playgroud)

SCCustomersVC.m(Modal VC)

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

SCCustomersVCDelegate.h

@class SCCustomer;

@protocol SCCustomersVCDelegate <NSObject>
@optional

- (void)passCustomer:(SCCustomer *)customer;

@end
Run Code Online (Sandbox Code Playgroud)

dan*_*anh 6

我想你差不多了.编辑 - 刚才在这里了解到,在iOS> 5中,viewWillAppear行为有所不同.您仍然希望视图将以模型状态更新您的视图,因为它需要在初始演示时执行此操作.

可以从模态vc或委托方法中调用它.因此,向viewWillAppear添加代码,使用视图控制器的模型状态更新视图...

// 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];
}
Run Code Online (Sandbox Code Playgroud)

然后从显示的vc或委托中调用viewViewWillAppear:

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