2016-01-07 33 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

相关问题