2014-01-10 157 views
-1

我真的很難找出爲什麼我的一個委託方法執行完美,但另一個不會。下面是委託相關的代碼不工作:委託方法不執行

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的例子中。它在原始代碼中。

+3

哪裏可以將「委託」設置爲您的'DetailReportViewController'對象? –

+1

我知道你有你的答案,但對於代表們 如果最佳實踐([self.delegate respondsToSelector:@selector(viewSelectMonitoredArea:)){ [self.delegate viewSelectMonitoredArea:self.selectedReport];} – Xeieshan

+0

邁克爾 - 我你的問題有點困惑。我以爲你在接收器ViewController,ala'sideBarController.delegate = self'中分配委託,而不是其他方式? – macboyrules

回答

1

DetailReportViewController類需要獲取對「SideBarController」實例的引用,然後將其自身設置爲委託。在DetailReportViewController:

// This needs to be in the DetailReportViewController class 
// Assume we have a reference to the SideBarController in the DetailReportViewController class called sideBarControllerInstance 
sideBarControllerInstance.delegate = self; 
+0

感謝您的評論。我知道這是一個簡單的錯誤,但我編輯了上面的代碼,其中包括參考。這是在原始代碼中,我在這裏複製代碼時犯了一個錯誤。 – macboyrules

1

邁克爾Dautermann刺激下的解決方案是把

DetailReportViewController *detailReport = [[DetailReportViewController alloc] init]; 
    self.delegate = detailReport; 

前右

[self.delegate createSelectMonitoredArea:self.selectedReport]; 

謝謝!