我真的很難找出爲什麼我的一個委託方法執行完美,但另一個不會。下面是委託相關的代碼不工作:委託方法不執行
SideBarController.h
#import <UIKit/UIKit.h>
#import "Report.h"
@protocol ReportSelectViewControllerDelegate <NSObject>
@optional
- (void) viewSelectMonitoredArea:(Report *)report;
@end
@interface SidebarController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, weak) id <ReportSelectViewControllerDelegate> delegate;
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, weak) Report *selectedReport;
@end
SideBarController.m
#import "SidebarController.h"
@interface SidebarController()
@end
@implementation SidebarController
@synthesize tableView = _tableView;
@synthesize delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *sectionArray = [self.tableViewItems objectAtIndex:indexPath.section];
NSDictionary* item = sectionArray[indexPath.row];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:item[@"title"] forKey:@"CurrentReportTitle"];
self.selectedReport = [Report where:@{@"reportTitle": item[@"title"]}].lastObject;
[self.delegate viewSelectMonitoredArea:self.selectedReport];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
DetailReportViewController.h
#import <UIKit/UIKit.h>
#import "BDBSplitViewController.h"
#import "SidebarController.h"
@interface DetailReportViewController : BDBDetailViewController <UISplitViewControllerDelegate, ReportSelectViewControllerDelegate >
@end
DetailReportViewController.m
#import "DetailReportViewController.h"
@interface DetailReportViewController()
@end
@implementation DetailReportViewController
- (void)viewDidLoad
{
[super viewDidLoad];
SidebarController *sideBarController = [[SidebarController alloc] init];
sideBarController.delegate = self;
}
- (void) viewSelectMonitoredArea:(Report *)report {
NSLog(@"viewSelectMonitoredArea was called");
}
從另一個控制器到DetailReportViewController書面方式完全相同另一位代表作品就好。當我在調用委託之後立即放置斷點時,我注意到委託沒有值。任何幫助將不勝感激。 編輯: 對不起,我忘了把sideBarController.delegate = self
放在DetailReportViewController.h的例子中。它在原始代碼中。
哪裏可以將「委託」設置爲您的'DetailReportViewController'對象? –
我知道你有你的答案,但對於代表們 如果最佳實踐([self.delegate respondsToSelector:@selector(viewSelectMonitoredArea:)){ [self.delegate viewSelectMonitoredArea:self.selectedReport];} – Xeieshan
邁克爾 - 我你的問題有點困惑。我以爲你在接收器ViewController,ala'sideBarController.delegate = self'中分配委託,而不是其他方式? – macboyrules