0

文件: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

文件: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

文件:PeopleLinkHeaderView.h

#import "PeopleLinkEditViewController.h"

@interface PeopleLinkHeaderView : UIView<PeopleLinkEditViewControllerDelegate>
{

}
@end

文件: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];
} 

无法调用委托的方法。当我调试它时,我发现在editcontroller中委托是零。和 editcontroller 由情节提要创建。Headerview 是编辑控制器的子视图。

4

1 回答 1

3

问题是您向其发送操作的实例与您为其声明委托的实例不同。

我可以通过看这个来判断

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

这是一个新创建的实例,您不会以任何方式显示或呈现它。也许这是在故事板上制作的不同视图控制器的形式?如果它是您在情节提要中指定的,您应该检索那个并分配它的委托。

使用它来检索正确的实例

#import ViewController.h

然后在要设置委托的地方。

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

不要忘记更改以包含类的标题并更改为正确的标签。

从这里检索:

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

于 2012-08-14T06:33:06.847 回答