我试图实现两个委托方法。我认为我已正确编写协议,并且我正确地分配了委托,因为我在另一个工作正常的委托/协议上仔细建模。但是,代表方法在被调用时不会触发,或者至少看起来像是在发生什么。我已经在调用委托方法的地方设置了断点,也在方法本身设置了断点,但似乎控件永远不会到达它们。
以下是我认为的相关代码。来自notifyingVC.h文件:
// AddCategoryVC.h
// YYYYYY
//
// Created by XXXXXX on 2/19/15.
// Copyright (c) 2015 XXXXXX. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "WMMGCategory.h"
//#import "AddTransactionVC.h"
@class AddTransactionVC;
@protocol AddCategoryDelegate <NSObject>
-(void) addCategoryDidSave : (WMMGCategory *) brandNewCategory;
-(void) addCategoryDidCancel : (WMMGCategory *) categoryToDelete;
@end
@interface AddCategoryVC : UIViewController
@property (nonatomic, weak) id <AddCategoryDelegate> delegate;
来自notifyingVC.m文件:
// AddCategoryVC.m
// YYYYYY
//
// Created by XXXXXX on 2/19/15.
// Copyright (c) 2015 XXXXXX. All rights reserved.
//
#import "AddCategoryVC.h"
#import "WMMGCategory.h"
@interface AddCategoryVC ()
@end
@implementation AddCategoryVC
…Blah de blah…
- (IBAction)saveCategory:(UIBarButtonItem *)sender
{
if (self.nooCatTextField.text.length < 1)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New category not identified"
message:@"Please enter a name for the new category or cancel"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
self.brandNewCategory.name = [self.nooCatTextField.text uppercaseString];
[self.delegate addCategoryDidSave:self.brandNewCategory];
}
}
- (IBAction)cancelCategory:(UIBarButtonItem *)sender
{
[self.delegate addCategoryDidCancel:self.brandNewCategory];
}
来自DelegateVC.h文件:
//
// AddTransactionVC.h
// YYYYYY
//
// Created by XXXXXX on 2/6/15.
// Copyright (c) 2015 XXXXXX. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MagicalRecord/CoreData+MagicalRecord.h>
#import "WMMGTransaction.h"
#import "AddCategoryVC.h"
#import "WMMGCategory.h"
来自DelegateVC.m标题:
//
// AddTransactionVC.m
// YYYYYY
//
// Created by XXXXXXXX on 2/6/15.
// Copyright (c) 2015 XXXXXX. All rights reserved.
//
#import "AddTransactionVC.h"
@interface AddTransactionVC ()
@end
@implementation AddTransactionVC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
AddCategoryVC *acvc = [[AddCategoryVC alloc]init];
acvc.delegate = self;
}
最后,从DelegateVC.m文件中,委托方法本身:
#pragma mark - New category delegate methods
-(void)addCategoryDidSave:(WMMGCategory *)brandNewCategory
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
[localContext MR_saveToPersistentStoreAndWait];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(void)addCategoryDidCancel:(WMMGCategory *)categoryToDelete
{
[categoryToDelete MR_deleteEntity];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
我在前面承认我可能会做出一些愚蠢的错字或其他什么,但我确实感谢有人看看我做错了什么。
编辑,根据@ rdelmar的答案如下:
这是AddCategoryVC实例的创建代码:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString *identifier = segue.identifier;
if ([identifier isEqualToString:@"CatSelectSegue"])
{
UIViewController *dvc = segue.destinationViewController;
UIPopoverPresentationController *catSelPPC = dvc.popoverPresentationController;
if (catSelPPC)
{
catSelPPC.delegate = self;
}
}
else if ([identifier isEqualToString:@"newCatSegue"])
{
UIViewController *dvc = segue.destinationViewController;
UIPopoverPresentationController *catNewPPC = dvc.popoverPresentationController;
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
if ([[segue identifier] isEqualToString:@"newTransSegue"])
{
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
AddCategoryVC *addCatVC = (AddCategoryVC *)navController.topViewController;
addCatVC.delegate = self;
WMMGCategory *addedCategory = (WMMGCategory *)[WMMGCategory MR_createInContext:localContext];
addCatVC.brandNewCategory = addedCategory;
}
if (catNewPPC)
{
catNewPPC.delegate = self;
}
}
}
答案 0 :(得分:2)
你的问题是你在viewDidLoad中创建了一个AddCategoryVC实例,它不是你在屏幕上拥有的实例(你从不对它做任何事情,所以它会在viewDidLoad超出范围后立即解除分配)。您需要获得对已有实例的引用。如果不知道你在哪里制作控制器以及它们如何在屏幕上显示,我就无法具体说明如何做到这一点。