简体   繁体   English

Objective-C-消除模态后重新加载表视图数据

[英]Objective-C - Reloading table view data after dismissing modal

I am trying to reload my table view controller after inserting an item using core data. 我尝试使用核心数据插入项目后重新加载我的表视图控制器。 I am using storyboards to present my modal view controller and am setting the delegate of the destination view controller in prepare to segue but i believe that is the issue, as my didAddCollection method is not being triggered. 我正在使用情节提要来演示我的模态视图控制器,并设置目标视图控制器的委托以准备进行搜索,但我认为这是问题所在,因为未触发我的didAddCollection方法。 I am not sure what i am missing here. 我不确定我在这里想念的是什么。 i used iOS TableView Reload after dismissing modal as the base for the current code i have. 在消除模态作为我当前代码的基础之后,我使用了iOS TableView Reload

In my CCNewCollectionViewController.h 在我的CCNewCollectionViewController.h中

@protocol NewCollectionDelegate <NSObject>

-(void) didAddCollection;

@end


@interface CCNewCollectionViewController : UIViewController {
    id delegate;
}

@property (nonatomic, retain) id <NewCollectionDelegate> delegate;

In my CCNewCollectionViewController.m 在我的CCNewCollectionViewController.m中

@implementation CCNewCollectionViewController

@synthesize delegate;

// save data

[self.delegate didAddCollection];

// dismiss view controller

In my CollectionTableViewController.m 在我的CollectionTableViewController.m中

@interface CollectionTableViewController () <NSFetchedResultsControllerDelegate, NewCollectionDelegate>

-(void) didAddCollection {

    [self.tableView reloadData];

}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    [[segue destinationViewController] setDelegate:self];
}

A very silly mistake. 一个非常愚蠢的错误。 When setting the delegate of the destination view controller, i was actually setting the delegate of the UINavigationController instead of the CCNewCollectionViewController. 在设置目标视图控制器的委托时,实际上是在设置UINavigationController的委托,而不是CCNewCollectionViewController。

UINavigationController *nc = [segue destinationViewController];
CCNewCollectionViewController *vc = nc.viewControllers[0];
[vc setDelegate:self];

I changed my prepareForSegue code to this and it solved my problems! 我将我的prepareForSegue代码更改为此,它解决了我的问题!

Not sure if this will solve your problem, but I think it is more typical to just create a weak delegate property. 不知道这是否可以解决您的问题,但我认为仅创建一个弱的委托属性是比较典型的。 No need to synthesize. 无需合成。

    @interface CCNewCollectionViewController : UIViewController
    @property (nonatomic,weak) id <NewCollectionDelegate> delegate;
    ...
    @end

See here for more information about working with protocols. 有关使用协议的更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM