簡體   English   中英

為什么不能調用委托方法

[英]why the method for delegate can't be called

檔案: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是編輯控制器的子視圖。

問題在於,您要將操作發送到的實例與您為其聲明委托的實例不相同。

我可以看一下

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

這是一個新創建的實例,您不會以任何方式顯示或展示它。 也許這是情節提要上制作的另一種View Controller? 如果是在情節提要中指定的一個,則應檢索該一個並分配其委托。

使用它來檢索正確的實例

#import ViewController.h

然后在您要設置代表的地方。

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

不要忘記更改以包括該類的標題並更改為正確的標記。

從這里檢索:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM