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

前端之家收集整理的这篇文章主要介绍了ios – 在通过委托解除其呈现的模态视图控制器后,在UIViewController中刷新数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
由于数据正在从模态传递到呈现视图控制器,因此我有代理工作.但是,呈现视图控制器没有显示从模态接收到的数据.我看了其他帖子,他们说使用委托/协议方法,但不解释如何/为什么呈现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

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];
}

SCCustomersVC.h(Modal VC)

#import "SCCustomersVCDelegate.h"

@class SCCustomerDetailVC;

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

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

@end

SCCustomersVC.m(Modal VC)

- (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

解决方法

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

从您的modal 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];
}

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

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

猜你在找的iOS相关文章