[英]iOS delegate Cancel call fails
I've got two view controllers, HomeViewController
and AddViewController
. 我有两个视图控制器, HomeViewController
和AddViewController
。 HomeViewController
is the delegate of AddViewController
. HomeViewController
是AddViewController
的委托。
Problem is that when I hit the "Cancel" button in AddViewController
, the call back to the delegate seems not to execute. 问题是,当我在AddViewController
单击“取消”按钮时,对委托的调用似乎未执行。 Symptomatically, the Cancel button behaves as if it were not even wired up. 从症状上讲,“取消”按钮的行为就好像没有连接一样。 Programmatically, breakpoints seem to indicate that control leaves the cancelButton
method, yet never reaches addViewControllerDidCancel
. 以编程方式,断点似乎表明控件离开了cancelButton
方法,但从未到达addViewControllerDidCancel
。
I believe everything is wired properly, and here's the relevant code: 我相信一切都已正确接线,这是相关代码:
From AddViewController.h: 从AddViewController.h:
#import <UIKit/UIKit.h>
#import "WMDGCategory.h"
#import "WMDGActivity.h"
@class HomeViewController;
@protocol AddViewControllerDelegate <NSObject>
-(void) addViewControllerDidSave;
-(void) addViewControllerDidCancel:(WMDGActivity *) activityToDelete;
@end
@interface AddViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic, weak) id <AddViewControllerDelegate> delegate;
From HomeViewController.h: 从HomeViewController.h:
@interface HomeViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate,AddViewControllerDelegate>
From HomeViewController.m: 从HomeViewController.m:
-(void) addViewControllerDidSave
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
[localContext MR_saveToPersistentStoreAndWait];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
[self refreshData];
}
-(void) addViewControllerDidCancel:(WMDGActivity *) activityToDelete
{
[activityToDelete MR_deleteEntity];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
[self refreshData];
}
And from AddViewController.m: 从AddViewController.m:
- (IBAction)cancelButton:(UIBarButtonItem *)sender
{
[self.delegate addViewControllerDidCancel:self.thisActivity];
}
Can someone spot my mistake? 有人可以发现我的错误吗?
Thanks! 谢谢!
Edit in response to answer 1 below: 根据以下答案1进行编辑:
Actually, I did this in my prepareFroSegue method in HomeViewController: 实际上,我是在HomeViewController的prepareFroSegue方法中完成此操作的:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
if ([[segue identifier] isEqualToString:@"addModal"])
{
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
AddViewController *avc = (AddViewController *)navController.topViewController;
avc.delegate = self;
WMDGActivity *addedActivity = (WMDGActivity *)[WMDGActivity MR_createInContext:localContext];
avc.thisActivity = addedActivity;
}
}
Your missing assigning the delegate object of your AddViewController instance, from what I can see it's still nil
您缺少分配AddViewController实例的委托对象,从我所看到的仍然是nil
myAddViewController.delegate = self; // 'self' is an example, should be myHomeViewController instance
EDIT: 编辑:
You are calling this in your code: 您在代码中调用此代码:
[self.delegate addViewControllerDidCancel:self.thisActivity];
Ask yourself what is the value of delegate
? 问自己, delegate
的价值是什么? You have declared it, yes, but you have not set its value, so it must be nil
. 您已经声明了它,是的,但是尚未设置其值,因此它必须为nil
。 So when you instantiate your AddViewController
you must set the delegate using the line I wrote before. 因此,当实例化AddViewController
,必须使用我之前写过的行来设置委托。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.