0

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.

user1361168
  • 73
  • 2
  • 7
  • Check while debugging, Has "delegate" initialize and has it assigned memory? and you can insert condition if(delegate != nil && [delegate respondsToSelector:@selector(headerInfoEditFinish)]). – alloc_iNit Aug 14 '12 at 06:26
  • YES, I check out that delegate is nil. but why? how to fix it? – user1361168 Aug 14 '12 at 06:32

1 Answers1

3

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

Community
  • 1
  • 1
user1536580
  • 132
  • 4
  • YES, this viewcontroller is ceated by storyboard. so this means that I should use storyboard tag to retrieve it ? – user1361168 Aug 14 '12 at 06:42
  • UIStoryboard* sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; PeopleLinkEditViewController *edit = [sb instantiateViewControllerWithIdentifier:@"PeopleLinkEditController"]; edit.delegate = self; using this code can't fix this problem. so do you have any suggestion, thanks – user1361168 Aug 14 '12 at 06:49
  • you can instantiate the storyboard with whatever tag you have by using the code in the edit – user1536580 Aug 14 '12 at 06:52
  • i don't know how to do it. would like to provide more information? – user1361168 Aug 14 '12 at 06:59
  • you know what, on the storyboard in wherever you set the viewcontroller that has the delegate property, control drag it to whatever viewcontroller you want it to be the delegate. (Look for the assigned last tab of the properties there should be the "delegate" so you can link it there. just drag the dot to the other viewcontroller) that works aswell – user1536580 Aug 14 '12 at 08:10
  • This is probably one of the best answers out there for this thing. Its also compatible with subviews in storyboards. I use this method for creating vertical "toolbars" made of other views. – rafinskipg May 28 '13 at 13:34