2016-01-07 32 views
0

當我試圖實現在主機和細節與NavigationController一個SplitViewController。我一直在關注this tutorial,但是我仍然遇到一個相當奇怪的問題。 當我嘗試撥打代表方法時,我收到-[UINavigationController selectedStudent:]: unrecognized selector sent to instance...無法識別的選擇調用委託方法

任何幫助都將大大受益。

下面的代碼:

StudentSelectionDelegate.h

#import <Foundation/Foundation.h> 
@class Student; 
@protocol StudentSelectionDelegate <NSObject> 
@required 
-(void)selectedStudent:(Student *)newStudent; 
@end 

StudentDetail表示在分割視圖的細節。 在StudentDetail.h I`ve得到

#import "StudentSelectionDelegate.h" 
@interface StudentDetail : UITableViewController <StudentSelectionDelegate> 
... 

StudentDetail.m

@synthesize SentStudent; 
... 
-(void)selectedStudent:(Student *)newStudent 
{ 
    [self setStudent:newStudent]; 
} 

StudentList表示SPLITVIEW的主人。在StudentList.h I`ve有:

#import "StudentSelectionDelegate.h" 
... 
@property (nonatomic,strong) id<StudentSelectionDelegate> delegate; 

在StudentList.m在didSelectRowAtIndexPath

[self.delegate selectedStudent:SelectedStudent]; 

而且沒有 「SelectedStudent」 不爲空

最後AppDelegate.m

#import "AppDelegate.h" 
#import "StudentDetail.h" 
#import "StudentListNew.h" 
... 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; 

    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; 
    UINavigationController *leftNavController = [splitViewController.viewControllers objectAtIndex:0]; 
    StudentListNew *leftViewController = (StudentListNew *)[leftNavController topViewController]; 
    StudentDetail *rightViewController = [splitViewController.viewControllers objectAtIndex:1]; 

    leftViewController.delegate = rightViewController; 

    return YES; 
} 

PS我一直在尋找解決方案几個小時。

+0

是'rightViewController'真的'StudentDetail'實例?當你調用'selectedStudent:'記錄委託的類,看看你是否有正確的對象類型。 –

+0

你有什麼原因重新發明輪子?Xcode Master-Detail應用程序模板已經正確地做到了這一點。 –

+2

@PetahChristian他在學習。重新創造輪子是學生做的。 – trojanfoe

回答

1

[splitViewController.viewControllers objectAtIndex:1]UINavigationController而不是StudentDetail

錯誤消息告訴您UINavigationController沒有selectedStudent屬性。

你的代表沒有指向StudentDetail,而是一個導航控制器,它甚至沒有實現< StudentSelectionDelegate>。但是,由於您指定了演員表,因此Objective C無法警告您所投射的對象實際上並不是您所投射的類別。

你應該考慮類型檢查的對象,如蘋果公司的代碼執行,以確保對象是你希望他們是類。

下面是更正後的代碼:

UINavigationController *rightNavController = [splitViewController.viewControllers objectAtIndex:1]; 
StudentDetail *rightViewController = (StudentDetail *)[rightNavController topViewController]; 
leftViewController.delegate = rightViewController; 

對於確保您的委託實現的方法,

if ([self.delegate respondsToSelector:@selector(selectedStudent:)]) { 
    [self.delegate selectedStudent:SelectedStudent]; 
} 

會饒了你從異常,但你必須已經使用調試器意識到self.delegate不是StudentDetail

+0

你先生是我的英雄! – Hadjimina

+0

@Hadjimina如果您有興趣瞭解其他將信息傳遞給視圖控制器的方法,請查看'prepareForSegue'。它可以用來代替委託和'didSelectRowAtIndexPath'代碼。 –

+0

這就是我在開始時所做的。然而,我沒有得到那個工作,並認爲代表的事情是更多的proffessionell – Hadjimina

相關問題