2014-10-19 39 views
0

我的应用程序委托方法中有一些代码应该将对象发送给视图控制器。出于某种原因,尽管它抛出一个错误:“无法识别的选择发送到实例”应用程序委托:发送到实例的无法识别的选择器

在视图控制器我有以下变量设置:

@property (nonatomic, strong) Patient* patient; 

在我的应用程序代理的代码如下所示:

UISplitViewController* splitViewController = (UISplitViewController*)self.window.rootViewController; 
UINavigationController* patientNavController = [splitViewController.viewControllers objectAtIndex:0]; 
PatientMasterTableViewController* patientMasterTableViewController = (PatientMasterTableViewController*)[patientNavController topViewController]; 
PatientDetailViewController* patientDetailViewController = [splitViewController.viewControllers objectAtIndex:0]; 

Patient* firstPatient = [[patientMasterTableViewController patientArray] objectAtIndex:0]; 
[patientDetailViewController setPatient:firstPatient];// this line throwing the error 

我是新来的iOS,我不太明白为什么它不会让我通过病人对象。谁能帮忙?

+0

您是否在头文件中设置了属性? – gabbler 2014-10-19 05:17:42

+0

添加您正在获取的异常的详细信息 - 它会告诉您选择器是什么以及您尝试将其发送到的对象类型。这将使你能够找出你出错的地方 – Paulw11 2014-10-19 05:29:52

+0

异常的细节: UINavigationController setPatient:]:无法识别的选择器发送到实例0x8fa1380 2014-10-19 15:58:08.136 ORA [946:60b] * **因未捕获异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UINavigationController setPatient:]:无法识别的选择器发送到实例0x8fa1380' – user2787386 2014-10-19 05:35:59

回答

1

这行代码:

PatientDetailViewController* patientDetailViewController = [splitViewController.viewControllers objectAtIndex:0]; 

不保证返回PatientDetailViewController。它可以返回任何类的对象,并且不检查返回的对象类。

你的应用崩溃了,因为它返回了一个UINavgationController对象,该对象没有setPatient方法。

至于为什么它返回错误类的对象,这将取决于您创建的视图控制器。

0

你在你的代码错误 - 看这两条线:

的UINavigationController * patientNavController = [splitViewController.viewControllers objectAtIndex:0];

PatientDetailViewController * patientDetailViewController = [splitViewController.viewControllers objectAtIndex:0];

数组splitViewController.viewControllers只包含一个!纵向模式下的元素和横向模式下的TWO元素。 DetailsViewController始终是这个数组中,但主并不:在纵向模式阵列只包含详细视图Ctrl键,在横向排列为@ [大师,详情]

所以如果你想永远得到DetailViewController使用此代码

[[splitViewController viewControllers] lastObject];

相关问题