【问题标题】:Refreshing data in a UIViewController after dismissing its presented modal view controller via delegate通过委托关闭其呈现的模式视图控制器后刷新 UIViewController 中的数据
【发布时间】:2013-03-18 04:42:45
【问题描述】:

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

【问题讨论】:

  • 在您的委托中获得正确参考后您做了什么。
  • @Zen 是的,很抱歉给大家带来了麻烦。出于某种原因,我认为它会在我疲倦的状态下昨晚“神奇地”自行更新。我需要添加代码来更新它。

标签: ios objective-c delegates protocols presentmodalviewcontroller


【解决方案1】:

我想你快到了。编辑 - 刚刚了解到here viewWillAppear 行为在 iOS>5 中是不同的。您仍然希望视图会显示为使用您的模型状态更新您的视图,因为它需要在初始演示时执行此操作。

可以从模态 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:^{}];
}

【讨论】:

  • 感谢 danh,我进行了更改,并在 viewWillAppear 中设置了一个断点,但没有成功。
【解决方案2】:

您应该在设置“客户”后重新加载您的用户界面

- (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?
  • 例如这样 label.text = customer.name
  • 您的线路例如对于 tableView,它将是 [tableView reload]; 解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 2021-05-21
  • 2020-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
相关资源
最近更新 更多