简体   繁体   中英

Why are my delegate methods never called?

I've tried to set up a very basic delegate between a TableViewController and a DetailViewController , but the methods are never called. Here's my code:

DetailViewController.h

@protocol DetailViewControllerDelegate

- (void) detailViewControllerDidLike;
- (void) detailViewControllerDidUnlike;
- (void) detailViewControllerDidDislike;

@end

DetailViewController.m

- (IBAction) changeLikedSwitch: (id) sender
{
    UISwitch *likedSwitch = (UISwitch *) sender;
    if ([likedSwitch isOn]) {
        [_selectedQuote setIsLiked: [NSNumber numberWithBool: YES]];
        [self.delegate detailViewControllerDidLike];
    } else {
        [_selectedQuote setIsLiked: [NSNumber numberWithBool: NO]];
        [self.delegate detailViewControllerDidUnlike];
    }

    NSError *error;
    if (![[[CDManager sharedManager] managedObjectContext] save:&error]) NSLog(@"Saving changes failed: %@, %@", error, [error userInfo]);
}

- (IBAction) changeDislikedSwitch: (id) sender
{
    UISwitch *dislikedSwitch = (UISwitch *) sender;
    if ([dislikedSwitch isOn]) {
        [_selectedQuote setIsDisliked: [NSNumber numberWithBool: YES]];
        [self.delegate detailViewControllerDidDislike];
        [self dismissViewControllerAnimated: YES completion: nil];
    } else {
        [_selectedQuote setIsDisliked: [NSNumber numberWithBool: NO]];
    }

    NSError *error;
    if (![[[CDManager sharedManager] managedObjectContext] save:&error]) NSLog(@"Saving changes failed: %@, %@", error, [error userInfo]);
}

TableViewController.h Interface line:

@interface TableViewController : UITableViewController <NSFetchedResultsControllerDelegate, DetailViewControllerDelegate>

TableViewController.m

- (void) detailViewControllerDidLike
{
    NSLog(@"detailViewControllerDidLike!");
    [self.tableView reloadData];
}

- (void) detailViewControllerDidUnlike
{
    NSLog(@"detailViewControllerDidUnlike!");
    [self.tableView reloadData];
}

- (void) detailViewControllerDidDislike
{
    NSLog(@"detailViewControllerDidDislike!");
    [self.tableView reloadData];
}

None of these methods are called. I'm trying to work out whether it's because I haven't set the delegate, but I don't understand how I can do that. There isn't an instance of my DetailViewController in my TableViewController , so how am I supposed to set one of its properties? Isn't the whole point of having a delegate that I don't need to create a concrete link between the classes? Very, very confused here.

You do need to set the delegate, for delegate methods to be called.

You must have a class that creates both the TableViewController and the DetailViewController? when they are created you would call

[myDetailViewControllerObject setDelegate:myTableViewControllerObject];

to set the delegate. This is assuming you've defined a delegate property in DetailViewController with

@property (readwrite, weak) id<DetailViewcontrollerDelegate> delegate;

A further explanation of delegates:

The point of a delegate is so that you don't need a specific type of object, you only need an object that implements the protocol. There still needs to be a connection between the delegate and the "delegator". If you want no concrete connection, then you would want to use an NSNotification, which is very much a "shout into the ether, and hope something is listening" method of communication.

In this case, a delegate is the correct thing to use. Delegates should be for one-to-one relationships, NSNotification are best used for one-to-N type relationships, where N can be 0 or more.

Maybe you forgot to set the delegate. In the prepareForSegue you write:

myDetailController.delegate = self;

Please make sure about following things for delegate.

Creating Property

@property(nonatomic, assign) id < SettingsViewControllerDelegate > delegate;

Synthesize it but do not release it

@synthesize delegate;

Set delegate on your object.

settingsViewController.delegate = self;

There are very vew situations where you should retain a delegate, normally your delagate will outlive the object. So change your property from retain to assign. And be sure you are setting the delegate. Where are you doing it? If your object really depends on it you should be passing it in the constructor ( iniWithDelegate ). Try doing a NSLog before calling the delagate method just to see if it isn't nil.

Hey,just have a look at this link . The linked helped me once. i hope it will help you too.

Add a delegate for DetailViewControllerDelegate in your TableViewController.h:

#import "DetailViewController.h"
@property (retain, nonatomic) id <DetailViewControllerDelegate>delegate;

Then in TableViewController.m:

@synthesize delegate;

Now call your delegate method as:

[delegate detailViewControllerDidLike];

Most important: Where you are adding TableViewController in your main view, don't forget to write:

taleViewControllerInstanceName.delegate=self;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM