简体   繁体   English

iOS代表取消通话失败

[英]iOS delegate Cancel call fails

I've got two view controllers, HomeViewController and AddViewController . 我有两个视图控制器, HomeViewControllerAddViewController HomeViewController is the delegate of AddViewController . HomeViewControllerAddViewController的委托。

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.

相关问题 MFMailComposeViewController的代表未处理“取消”按钮或iOS 4.x及更早版本 - MFMailComposeViewController's delegate not handling the 'Cancel' button or iOS 4.x and earlier 无法在UITableView实现中调用委托/数据源方法 - Fails to call delegate/datasource methods in UITableView implementation ABPeoplePickerNavigationController将“取消”按钮文本更改为“完成”,仍然调用委托方法吗? - ABPeoplePickerNavigationController change Cancel button text to Done and still call delegate method? FB返回且委托已取消分配后,取消委托调用 - Cancel delegate call after FB returned and delegate has been dealloc'd iOS:CollectionView里面的ScrollView委托方法没有调用 - iOS : ScrollView delegate method inside of CollectionView not call 如何在iOS中调用UITableView的ScrollViewDidScroll委托方法 - How to call ScrollViewDidScroll delegate method of UITableView in IOS iOS键盘语言更改时是否有委托调用? - Is there a delegate call when iOS keyboard language changes? 当我们在iOS中使用委托和回电时? - When we use delegate and Call back in iOS? 在 iOS 的应用程序委托中调用 api 是一种好方法吗? - Is it good method to call api in app delegate in iOS? iOS-MBProgressHUD-JSON异步调用-应用程序委托 - iOS - MBProgressHUD - JSON Asynch call - Application Delegate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM