Shawn Rong
Shawn Rong

Reputation: 145

Why my custom delegate didn't work in Objective-C?

Here's my code. AddItemViewController.h:

#import <UIKit/UIKit.h>

@class AddItemViewController, ChecklistItem;
@protocol AddItemViewControllerDelegate <NSObject>
@optional
- (void)addItemViewControllerDidCancel:(AddItemViewController *)controller;
- (void)addItemViewController:(AddItemViewController *)controller didFinishAddingItem:(ChecklistItem *)item;
@end

@interface AddItemViewController : UITableViewController
@property (weak, nonatomic) id<AddItemViewControllerDelegate> delegate;
@end

AddItemViewController.m:

#import "AddItemViewController.h"
#import "ChecklistItem.h"

@interface AddItemViewController () <UITextFieldDelegate>
- (IBAction)Done:(id)sender;
- (IBAction)Cancel:(id)sender;

@end
...
- (IBAction)Done:(id)sender {
    ChecklistItem *item = [[ChecklistItem alloc]init];
    item.text = self.textField.text;
    item.checked = false;

    if ([self.delegate respondsToSelector:@selector(addItemViewController:didFinishAddingItem:)]) {
        [self.delegate addItemViewController:self didFinishAddingItem:item];
    }
}

- (IBAction)Cancel:(id)sender {
    [self.delegate addItemViewControllerDidCancel:self];
}

...

and the ChecklistViewController call the delegate. ChecklistViewController.m:

#import "ChecklistViewController.h"
#import "ChecklistItem.h"
#import "AddItemViewController.h"

@interface ChecklistViewController () <UITableViewDelegate, UITableViewDataSource, AddItemViewControllerDelegate>
@end

@implementation ChecklistViewController
...
- (void)viewDidLoad {
    [super viewDidLoad];
    AddItemViewController *addItem = [[AddItemViewController alloc]init];
    addItem.delegate = self;    
}
- (void)addItemViewControllerDidCancel:(AddItemViewController *)controller{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)addItemViewController:(AddItemViewController *)controller didFinishAddingItem:(ChecklistItem *)item{
    [self dismissViewControllerAnimated:YES completion:nil];
}
...
@end

I've debug the app.When the AddItemViewController runs,the 'self.delegate' is null.Anyone can tell me what's wrong with my code please.Thank you vert much!

Upvotes: 0

Views: 204

Answers (4)

meda
meda

Reputation: 45500

I'm not sure how you present the AddItemViewController it's not in your code.

All you have in viewDidLoad is :

AddItemViewController *addItem = [[AddItemViewController alloc]init];
addItem.delegate = self; 

But then you don't present it , so it makes me think that it's not normal because it is local to the viewload function.

Also your are using alloc/init which is not usual for ViewController unless you are programmatically giving it nib file which I doubt you are.

Most Likely You should assign the self delegate at the moment where you segue or present that controller.

Upvotes: 0

Robin.Chao
Robin.Chao

Reputation: 113

Did you assign the delegate in ChecklistViewController? if didn't set the AddItemViewController's delegate is ChecklistViewController instance, the delegate methods will not call. Like this:

Hope it help.

Upvotes: 1

tuledev
tuledev

Reputation: 10327

Make sure that you already assign the delegate.

Beside that, you should check before let the delegate do anything:

- (IBAction)Cancel:(id)sender {
    if ([self.delegate respondsToSelector:@selector(addItemViewControllerDidCancel:)]) {
         [self.delegate addItemViewControllerDidCancel:self];
    }
}

Upvotes: 0

Clint Martin
Clint Martin

Reputation: 231

Delegates are weakly held. You've set that up correctly with

@property (weak, nonatomic) id<AddItemViewControllerDelegate> delegate;

But, since it is weakly held, something else is going to have to hold a reference to that delegate object.

Also, I don't see anywhere in the code that you've posted where you are assigning the delegate object. So, a couple of things to try:

  1. Make sure you assign the delegate (AddItemViewController instance.delegate = ChecklistViewController instance)
  2. Make sure something else is referencing your ChecklistViewController so it doesn't go away when you need it.

Upvotes: 3

Related Questions