Implementing delegation

2019-05-18 16:30发布

问题:

I think I'm following how delegation works, here's the tutorial I followed, but I'm messing up somewhere. I'm expecting my delegate to NSLog but it's not. Can anyone find out what am I missing or doing wrong?

My MainViewController.h:

@interface MainViewController : UITableViewController < AddClassDelegate >

MainViewController.m:

- (void)cancelAddingClass {
    NSLog(@"Canceled Yo");
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

/*
 When a row is selected, the segue creates the detail view controller as the destination.
 Set the detail view controller's detail item to the item associated with the selected row.
 */
if ([[segue identifier] isEqualToString:@"addClassSegue"]) {

    UINavigationController *nc = (UINavigationController *)segue.destinationViewController;
    AddClassViewController *addClassVC = (AddClassViewController *)[nc.viewControllers objectAtIndex:0];
    addClassVC.delegate = self;
}

My modal view controller AddClassViewController.h:

@protocol AddClassDelegate <NSObject>
- (void)cancelAddingClass;
@end

@interface AddClassViewController : UITableViewController

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

- (IBAction)cancelButtonPressed:(id)sender;

AddClassViewController.m:

@synthesize delegate;

- (IBAction)cancelButtonPressed:(id)sender {
    [self.delegate cancelAddingClass];
}

cancelButtonPressed: 

is hooked up to the modal view's Cancel button in Storyboard.

回答1:

Your code looks fine, which suggests the problem is somewhere we can't see. My guess is here:

AddClassViewController *addClassVC = [segue destinationViewController];
addClassVC.delegate = self;
NSLog(@"segued");

Have you embedded your modal view controller in a navigation controller? If so, destinationViewController gives you the navigation controller, not the AddClassViewController. Check what class addClassVC actually is in the debugger.

If it is a navigation controller, no problem, you just need to get to your actual VC using the .viewControllers property. On several lines to make it simpler to understand:

UINavigationController *nc = (UINavigationController *)segue.destinationViewController;
AddClassViewController *addClassVC = (AddClassViewController *)[nc.viewControllers objectAtIndex:0];
addClassVC.delegate = self;

You can do it in fewer lines but it's a mess of casting and nested brackets, which is harder to debug.



回答2:

Is it possible your main view controller is being released? That would set the weak reference to nil and the message you send to your delegate would simply be ignored because you'd be messaging nil.



回答3:

Everything is perfect. I haven't seen any issue. Keep break points everywhere and debug it.