通过委托关闭UIViewController的模态视图控制器后刷新数据

11

我已经让委托方法起作用了,数据从模态视图传递到呈现的视图控制器。但是呈现的视图控制器没有显示从模态视图接收到的数据。我看了其他帖子,它们说要使用委托/协议方法,但没有解释呈现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(模态 VC)

#import "SCCustomersVCDelegate.h"

@class SCCustomerDetailVC;

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

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

@end

SCCustomersVC.m(模态视图控制器)

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

在您的委托中获取正确引用后,您做了什么? - Zen
@Zen 是的,非常抱歉给大家带来麻烦。昨晚我很累,以为它会自己“神奇地”更新。我需要添加代码来更新它。 - abc123
2个回答

6
我认为你已经接近成功了。编辑 - 刚刚在这里学到,在iOS>5中,viewWillAppear的行为不同。你仍然希望使用 view will appear 根据您的模型状态更新视图,因为它需要在初始呈现时执行该操作。
从您的模态视图控制器或委托方法中调用它都可以。因此,请添加代码到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];
}

然后从显示的视图控制器或委托中调用viewWillAppear:方法:
- (void)passCustomer:(SCCustomer *)customer
{
    self.customer = customer;
    [self viewWillAppear:YES];
    [self dismissViewControllerAnimated:YES completion:^{}];
}

1
谢谢danh,我已经做出了更改,并在viewWillAppear中设置了断点,但它没有被触发。 - abc123

3
在设置“customer”后,您应该重新加载用户界面。
- (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];
        }];
}

重新加载不使用表格视图(仅文本标签)的 UI 的代码是什么? - user1904273
标签.文本 = 客户.姓名 - Evgeniy Gushchin
你的代码 例如对于tableView,它将是[tableView reload]; 解决了我的问题。 - R15

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接