Ios [UITabBarController setSports:]:发送到实例0x7b80e2b0的选择器无法识别
我正在构建一个iOS应用程序。我想通过segue将数组值从一个视图控制器传递到另一个视图控制器 执行此操作时,我遇到一个错误: [UITabBarController setSports:]:无法识别的选择器发送到 实例0x7b80e2b0 这是我的密码:Ios [UITabBarController setSports:]:发送到实例0x7b80e2b0的选择器无法识别,ios,objective-c,nsmutablearray,uitabbarcontroller,segue,Ios,Objective C,Nsmutablearray,Uitabbarcontroller,Segue,我正在构建一个iOS应用程序。我想通过segue将数组值从一个视图控制器传递到另一个视图控制器 执行此操作时,我遇到一个错误: [UITabBarController setSports:]:无法识别的选择器发送到 实例0x7b80e2b0 这是我的密码: -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"sports
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"sportsSelection"]) {
Play *play=[segue destinationViewController];
play.sports=selectedSports;//error break point is here.
//sports and selected sports are NSMutableArray
}
}
您需要
设置端口:
到视图控制器,而不是UITabBarController
Play* p = ((UITabBarController*)segue.destinationViewController).viewControllers[0];
p.sports = selectedSports;
为了让情况更清楚一点,出现错误是因为您试图对未实现此方法的类调用方法(
setSports:
),这正是错误消息告诉您的:
[UITabBarController setSports:]:无法识别的选择器发送到
实例0x7b80e2b0
您的segue
显然有一个UITabBarController
作为目标,因此它显然不知道方法setSports:
,因为这个方法实际上是在您的自定义视图控制器中实现的(显然称为Play
?!)
然后,正如艾克在他的回答中指出的那样,您需要从它所嵌入的UITabBarController
获取Play
。这就是为什么他建议使用:Play*p=((UITabBarController*)segue.destinationViewController.viewControllers[0]代码>,这意味着您应该从UITabBarController
获取索引为0的视图控制器
Play* p = ((UITabBarController*)segue.destinationViewController).viewControllers[0];
p.sports = selectedSports;
根据您的评论,索引0处的视图控制器是一个UINavigationController
,它(自然)也不响应setSports:
,因为就像UITabBarController
一样,它是苹果提供的一个类,不知道这种方法
现在,您需要找出自定义视图控制器Play
在该uitabarcontroller
中的位置。或者它是UITabBarController
的直接部分,您可以使用Eike的方法找到它,只需将索引从0修改为n(其中n是UITabBarController
拥有的视图控制器的数量),或者另一个选项是,它嵌入在索引0处收到的UINavigationController
中,因此在这种情况下,您必须访问UINavigationController
的视图控制器堆栈(例如,数组属性viewControllers
,或仅使用topViewController
)位于堆栈顶部的属性)
编辑:我想给你一些关于代码中发生了什么的额外信息,特别是与Eike的答案相关的信息:
根据您在问题和艾克解决方案通知中提供的信息,我们可以假设以下代码是正确的:
if ([[segue identifier] isEqualToString:@"sportsSelection"]) {
UITabBarController *tabBarController = [segue destinationViewController]; // the destination of the segue is your `UITabBarController`
UINavigationController *navigationController = tabBarController.viewControllers[0]; // gets the first of the view controllers contained in your UITabBarController
NSLog(@"view controllers in navigation controller: %@; top view controller: %@", navigationController.viewControllers, navigationController.topViewController); // print all view controllers managed by navigationController
}
if ([[segue identifier] isEqualToString:@"sportsSelection"]) {
UITabBarController *tabBarController = [segue destinationViewController]; // the destination of the segue is your `UITabBarController`
UINavigationController *navigationController = tabBarController.viewControllers[0]; // gets the first of the view controllers contained in your UITabBarController
Play *controller = (Play *)[[navigationController viewControllers] objectAtIndex:0];
controller.sports=selectedSports;
play.sports = selectedSports;
}
编辑2:根据您的评论,我现在可以假设以下代码是正确的:
if ([[segue identifier] isEqualToString:@"sportsSelection"]) {
UITabBarController *tabBarController = [segue destinationViewController]; // the destination of the segue is your `UITabBarController`
UINavigationController *navigationController = tabBarController.viewControllers[0]; // gets the first of the view controllers contained in your UITabBarController
NSLog(@"view controllers in navigation controller: %@; top view controller: %@", navigationController.viewControllers, navigationController.topViewController); // print all view controllers managed by navigationController
}
if ([[segue identifier] isEqualToString:@"sportsSelection"]) {
UITabBarController *tabBarController = [segue destinationViewController]; // the destination of the segue is your `UITabBarController`
UINavigationController *navigationController = tabBarController.viewControllers[0]; // gets the first of the view controllers contained in your UITabBarController
Play *controller = (Play *)[[navigationController viewControllers] objectAtIndex:0];
controller.sports=selectedSports;
play.sports = selectedSports;
}
Play是你的UIViewController类吗?它是UITableViewController,但它是UIAbbarController的一部分[segue destinationViewController]在你的情况下返回UIAbbarController。例外情况是tabController类没有名为“SetPorts”的选择器。我知道了,但你能帮我解决这个问题吗?这是“运动”“播放”控制器中的合成属性?谢谢@Eike我应用了你的代码,但得到了相同的错误[UINavigationController设置端口:]:发送到实例0x78F132B0的无法识别的选择器能否从情节提要中附加相关VCs的屏幕截图?是的,这肯定会help@Daljeet您没有收到相同的错误,这次它表示未识别的选择器被发送到UINavigationController
的实例,而不是uitabarcontroller代码>谢谢@nburk通过实现您的代码或nslog,我正在玩viewcontroller和topviewcontroller,但我发现代码中到处都没有设置端口:代码中的方法。我不知道下一步该怎么办我更新了答案,您的Play
视图控制器是UINavigationContro的topviewcontroller
您从UITabBarController
设置端口收到的ller
是您在Play
:)中声明的sports
属性的设置器。谢谢@nburk您的回答对我很有帮助,但我做了一点小小的改变:Play*controller=(Play*)[[navigationController ViewController]objectAtIndex:0];controller.sports=selectedSports;