简体   繁体   中英

why the method for delegate can't be called

File:PeopleLinkEditViewController.h

@protocol  PeopleLinkEditViewControllerDelegate<NSObject>
@optional
-(void)headerInfoEditFinish;

@end
@interface PeopleLinkEditViewController : UITableViewController
{
    id<PeopleLinkEditViewControllerDelegate> delegate;
}

@property (nonatomic, retain) id<PeopleLinkEditViewControllerDelegate> delegate;
-(IBAction)doneEdit:(id)sender;

@end

File:PeopleLinkEditViewController.m


@implementation PeopleLinkEditViewController
...
@synthesize delegate = _delegate;
...
- (void)viewDidLoad
{
...
headerView = [[PeopleLinkHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 286)
                                                    passData:headerDic];
...
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return headerView;
    }

    return nil;
}
-(IBAction)doneEdit:(id)sender
{      
    if ([delegate respondsToSelector:@selector(headerInfoEditFinish)])
    {
        NSLog(@"%d", __LINE__);
        [delegate headerInfoEditFinish];
    }
}
@end

File:PeopleLinkHeaderView.h

#import "PeopleLinkEditViewController.h"

@interface PeopleLinkHeaderView : UIView<PeopleLinkEditViewControllerDelegate>
{

}
@end

File:PeopleLinkHeaderView.m

@interface PeopleLinkHeaderView()

@property (nonatomic, retain) PeopleLinkEditViewController *edit;

@end

@implementation PeopleLinkHeaderView

- (id)initWithFrame:(CGRect)frame passData:(NSDictionary *)data
{
    UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                  bundle:nil];
    PeopleLinkEditViewController *edit = [sb instantiateViewControllerWithIdentifier:@"PeopleLinkEditController"];
    edit.editDelegate = self;
}

-(void)headerInfoEditFinish
{
    [baseInfo setValue:baseInfoValue forKey:@"value"];
    [dataPass writeHeaderValueToPlist:baseInfo];
} 

the method for delegate can't be called. And when I debug it, I find delegate is nil in editcontroller. and editcontroller is created by storyboard. Headerview is a subview of edit controller.

The problem is that the Instance you are sending the action to and the one you have declared the delegate for are not the same.

I can tell by looking at this

edit = [[PeopleLinkEditViewController alloc] init];
edit.delegate = self;

This is a newly created instance and you are not displaying or presenting it in any way. Perhaps this is form a different View Controller made on the storyboard? If it is one you specified in the storyboard you should retrieve THAT one and assign its delegate.

Use this to retrieve the correct instance

#import ViewController.h

Then on the place where you want to set the delegate.

ViewController *tmp = [[self storyboard] instantiateViewControllerWithIdentifier:@"ViewControllerIdentifier"];
tmp.delegate = self;

Dont forget to change to include the header for the class and change to the correct tag.

Retrieved from here:

https://stackoverflow.com/a/11931714/1068522

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