我应该如何正确执行此委托?

Posted

技术标签:

【中文标题】我应该如何正确执行此委托?【英文标题】:How should I perform this delegate properly? 【发布时间】:2014-11-29 14:43:27 【问题描述】:

我有一个表视图控制器和一个视图控制器。

StackTableViewController - 字符串列表

HomeViewController - 带有标签的空视图控制器

HomeViewController 标签应始终显示 StackTableViewController 的第一个字符串。

我需要确定第一个字符串是否被删除以呈现新的第一个字符串。

这就是我遇到问题的地方...如果我删除第一个字符串并返回 HomeViewController,标签仍然是我刚刚删除的字符串...如果我终止应用程序并再次打开它,标签中显示的正确字符串。

到目前为止,我是这样做的:

这是我的 StackTableViewController.h + .m 中的相关方法:

@protocol StackTableViewControllerDelegate <NSObject>

@optional

-(void)didDeleteObject;

@end

@interface StackTableViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate> 

@property (strong,nonatomic) id<StackTableViewControllerDelegate> delegate;
@property (strong, nonatomic) NSString *currentTarget;

@end



#import "StackTableViewController.h"
#import "Target.h"
#import "StackTableViewCell.h"
#import "HomeViewController.h"
#import "CoreDataStack.h"

@interface StackTableViewController () <NSFetchedResultsControllerDelegate>

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultController;

@end

@implementation StackTableViewController

- (id)init 

    self = [super initWithNibName:@"StackTableViewController" bundle:nil];
    if (self) 
        // Do something
        [self.fetchedResultController performFetch:nil];
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];
        self.currentTarget = current.body;
    
    return self;


- (void)viewDidLoad 

    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];
    self.currentTarget = current.body;


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

    Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];
    CoreDataStack *stack = [CoreDataStack defaultStack];
    [[stack managedObjectContext] deleteObject:target];
    [stack saveContext];

    if ([_delegate respondsToSelector:@selector(didDeleteObject)]) 
        [_delegate didDeleteObject];
    


这是HomeViewController.h + .m中的相关方法:

#import <UIKit/UIKit.h>
#import "StackTableViewController.h"

@interface HomeViewController : UIViewController 

    StackTableViewController *stackTableViewController;


@property (strong, nonatomic) IBOutlet UILabel *homeLabel;

- (IBAction)goToStack:(id)sender;



#import "StackTableViewController.h"

@interface HomeViewController () <StackTableViewControllerDelegate>


@end

@implementation HomeViewController

- (id)init 
    self = [super initWithNibName:@"HomeViewController" bundle:nil];
    if (self) 
        // Do something
        stackTableViewController = [[StackTableViewController alloc] init];
        stackTableViewController.delegate = self;
    
    return self;


- (void)viewDidLoad 

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationController setNavigationBarHidden:YES];
    self.homeLabel.font = [UIFont fontWithName:@"Candara-Bold" size:40];

    self.homeLabel.text = stackTableViewController.currentTarget;


- (void)didDeleteObject 
    self.homeLabel.text = stackTableViewController.currentTarget;


- (IBAction)goToStack:(id)sender 
    StackTableViewController *vc = [[StackTableViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];

CoreDataStack.h +.m:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface CoreDataStack : NSObject

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

+ (instancetype)defaultStack;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;


@end


#import "CoreDataStack.h"

@implementation CoreDataStack

#pragma mark - Core Data stack

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

+ (instancetype)defaultStack 

    static CoreDataStack *defaultStack;
    static dispatch_once_t onceTocken;
    dispatch_once (&onceTocken, ^
        defaultStack = [[self alloc] init];
    );

    return defaultStack;



- (NSURL *)applicationDocumentsDirectory 
    // The directory the application uses to store the Core Data store file. This code uses a directory named "digitalCrown.Treats" in the application's documents directory.
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];


- (NSManagedObjectModel *)managedObjectModel 
    // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
    if (_managedObjectModel != nil) 
        return _managedObjectModel;
    
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Treats" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) 
        return _persistentStoreCoordinator;
    

    // Create the coordinator and store

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Treats.sqlite"];
    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the application's saved data.";
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) 
        // Report any error we got.
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
        dict[NSLocalizedFailureReasonErrorKey] = failureReason;
        dict[NSUnderlyingErrorKey] = error;
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    

    return _persistentStoreCoordinator;



- (NSManagedObjectContext *)managedObjectContext 
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
    if (_managedObjectContext != nil) 
        return _managedObjectContext;
    

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator) 
        return nil;
    
    _managedObjectContext = [[NSManagedObjectContext alloc] init];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    return _managedObjectContext;


#pragma mark - Core Data Saving support

- (void)saveContext 
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) 
        NSError *error = nil;
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) 
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        
    



@end

请帮我解决这个问题,我正在尝试各种方法,但可能我错过了与视图控制器生命周期或其他东西有关的东西。

(CoreDataStack 是一个单例)

tnx!!

【问题讨论】:

【参考方案1】:

您可以做的是使用一个 Action 来检查发件人和 segue 以了解它来自哪里:

  - (void)unwindFromModal:(UIStoryboardSegue *)unwindSegue 
        if unwindSegue.identifier == @"createModal" 
             NSLog(@"Got home from create page");
        

        if unwindSegue.identifier == @"stackModal" 
           NSLog(@"Got home from stack page");
        
    


    - (IBAction)presentModal:(UIButton*)sender 
       if sender.tag == 0 
            [self performSegueWithIdentifier:@"createModal" sender:self];
        
       if sender.tag == 1 
            [self performSegueWithIdentifier:@"stackModal" sender:self];
        

    

你可以在标签中使用字符串常量来增加可读性,如果你也喜欢的话,还可以切换

【讨论】:

else if没问题【参考方案2】:

这里可能需要解决两个问题。

    根本不调用委托方法didDeleteObject。在goToStack 方法中,您创建了一个新的StackTableViewController,但忘记设置它的委托,同时您还有一个全局变量stackTableViewController 实例。我认为您的意思是将全局变量推入堆栈。

    commitEditingStyle tableview 委托方法中,您忘记更新用于更新标签文本的currentTarget 属性,您可以尝试更新该属性,看看会发生什么。

对于 1,您可以像这样更改 goToStack 函数:

[self.navigationController pushViewController:stackTableViewController animated:YES];

这意味着应该删除StackTableViewController *vc = [[StackTableViewController alloc] init];

对于 2,在commitEditingStyle 方法中,在[stack saveContext] 之后。

[stack saveContext];

//Please add the following code.
NSIndexPath *theIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
Target *current = [self.fetchedResultController objectAtIndexPath:theIndexPath];
self.currentTarget = current.body;

【讨论】:

#2 没用,你能提供更多关于#1 的细节吗?谢谢 抱歉,这两种解决方案都不起作用。当我去堆栈,删除第一个对象,然后点击“返回”时,homeLabel 仍然是我刚刚删除的对象的字符串......当我将终止应用程序并再次打开它时,标签中的数据将是正确 太奇怪了,没有人给我一个有效的答案......我认为这应该很简单...... commitEditingStyle的开头,加上这行NSLog(@"before deletion %lu", self.fetchedResultsController.fetchedObjects.count);,在saveContext之后,加上这行,NSLog(@"after deletion %lu", self.fetchedResultsController.fetchedObjects.count);,打印出current.body的值,看是不是已更新。 即4之前,3之后【参考方案3】:

您的问题是您没有在commitEditingStyle 中更新StackTableViewController 属性currentTarget。只需更新StackTableViewController.m中的代码:

此外,如果您使用相同的代码超过 2 次 - 最好将其移动到单独的函数中

- (id)init 

    self = [super initWithNibName:@"StackTableViewController" bundle:nil];
    if (self) 
        // Do something
        [self.fetchedResultController performFetch:nil];
        [self updateCurrentTarget];
    
    return self;


- (void)viewDidLoad 

    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self updateCurrentTarget];


- (void)updateCurrentTarget

    if (self.fetchedResultsController.sections.count == 0 && [self.fetchedResultsController.sections.firstObject count] == 0) 
        self.currentTarget = nil;
     else 
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];
        self.currentTarget = current.body;
    


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

    Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];
    CoreDataStack *stack = [CoreDataStack defaultStack];
    [[stack managedObjectContext] deleteObject:target];
    [stack saveContext];
    [self updateCurrentTarget];

    if ([_delegate respondsToSelector:@selector(didDeleteObject)]) 
        [_delegate didDeleteObject];
    

【讨论】:

【参考方案4】:

您需要进行 2 处更改:

goToStack: 中,在pushViewController: 之前添加vc.delegate = self 在您的StackTableViewController tableView: commitEditingStyle: 更新currentTarget 之前调用[_delegate didDeleteObject]

【讨论】:

我的问题是在堆栈视图控制器中我没有弹出视图控制器,所以在我添加后 viewWillDisappear [self.navigationController popToRootViewControllerAnimated:YES];问题解决了……【参考方案5】:
- (IBAction)goToStack:(id)sender 
    StackTableViewController *vc = [[StackTableViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];

这个方法应该如下:

- (IBAction)goToStack:(id)sender 
    StackTableViewController *vc = [[StackTableViewController alloc] init];
    vc.delegate = self.
    [self.navigationController pushViewController:vc animated:YES];

原因是您在 init 方法中将 HomeVC 设置为 StackTable 的委托,但您没有使用相同的 StackTable 实例并初始化新实例。

【讨论】:

以上是关于我应该如何正确执行此委托?的主要内容,如果未能解决你的问题,请参考以下文章

如何正确地将 3rd 方库委托转换为 RxSwift Observable

如何弱链接 iOS 应用程序委托方法?

在 Qt 中,如何正确实现委托?

Typescript:如何正确处理 axios 请求?

如何正确保留对 ActionSheet 委托使用的对象的引用?

使用自定义转场执行自定义转换的正确方法 - 没有副作用