xcode iOS - 错误:无法识别的选择器发送到 PrepareForSegue 中的实例:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22322443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - Error : unrecognized selector sent to instance in PrepareForSegue:
提问by user3405229
I wanna to pass data between controller, but I am getting this error, and I am blocked here :s
我想在控制器之间传递数据,但我收到了这个错误,我在这里被阻止了 :s
Error :
错误 :
[UITabBarController setUID:]: unrecognized selector sent to instance
Code :
代码 :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString *test = (NSString *)sender;
if ([segue.identifier isEqualToString:@"segueno"]) {
FirstViewController *VC = (FirstViewController *)[segue destinationViewController];
VC.uID = test;
NSLog(@"%@",VC.uID);
}}
Here is storyboaard
这是故事板
回答by Nikita Took
Check what is actual type of VC during runtime. Looks like you cast to wrong type.
在运行时检查 VC 的实际类型是什么。看起来您转换为错误的类型。
To check real type during runtime set breakpoint on line VC.uID = test;
. In debug window you should have something like:
要在运行时设置断点在线期间检查真实类型VC.uID = test;
。在调试窗口中,您应该有以下内容:
VC = (RealClass *) 0x312321312
Real class is not what you expected.
真正的课堂不是你所期望的。
The most possible reason, that VC
is UITabBarController
. So you have to replace
最可能的原因,VC
就是UITabBarController
。所以你必须更换
FirstViewController *VC = (FirstViewController *)[segue destinationViewController];
with
和
UITabBarController *tabBarController = (UITabBarController *)[segue destinationViewController];
// replace 1 with real index of your FirstVC
FirstViewController *VC = [[tabBarController viewControllers] objectAtIndex:1];
回答by Vijay-Apple-Dev.blogspot.com
segue destination is UITabBarController. so you need to check viewcontrollers list. then based on that u can look for uID.
segue 目标是 UITabBarController。所以你需要检查视图控制器列表。然后基于此您可以查找 uID。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString *test = (NSString *)sender;
if ([segue.identifier isEqualToString:@"segueno"]) {
FirstViewController *firstVC ;
UITabBarController *destinat = [segue destinationViewController];
if ([destinat isKindOfClass:[UITabBarController class]]) {
for (id listOfViewControllers in [destinat viewControllers]) {
if ([listOfViewControllers isKindOfClass:[FirstViewController class]]) {
firstVC = listOfViewControllers;
break;
}
}
}
firstVC.uID = test;
NSLog(@"%@",firstVC.uID);
}}
回答by Jeef
Here is how I pass managedObjectContext between segues
这是我如何在 segues 之间传递 managedObjectContext
In your class where you will pass the data use prepareForSegue call. (The assumption is this class has a variable called _managedObjectContextwhich can be passed along to the segue class)
在您将传递数据的课程中,使用 prepareForSegue 调用。(假设这个类有一个名为_managedObjectContext的变量,它可以传递给 segue 类)
Class to Segue From:
转接班级:
.h file:
.h 文件:
@property (weak, nonatomic) NSManagedObjectContext *managedObjectContext;
.m file:
.m 文件:
@synthesize managedObjectContext
The call to @synthesize will make the following:
对@synthesize 的调用将执行以下操作:
- a local variable called _managedObjectContext
- a method to getManagedObjectContext
- a method to setManagedObjectContext
- 一个名为_managedObjectContext的局部变量
- getManagedObjectContext的方法
- 一种设置ManagedObjectContext的方法
Additionally add the following method to your class
另外将以下方法添加到您的类中
// Pass on managedObjectContext
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// If the destination VC is able to take the setManagedObjectContext method the current objectContext will be passed along.
if ([segue.destinationViewController respondsToSelector:@selector(setManagedObjectContext:)]) {
[segue.destinationViewController performSelector:@selector(setManagedObjectContext:)
withObject:_managedObjectContext];
} else {
NSLog(@"Segue to controller [%@] that does not support passing managedObjectContext", [segue destinationViewController]);
}
}
Then in my "class" to receive the data I do: in the .h file i have
然后在我的“类”中接收我所做的数据:在 .h 文件中我有
@property (weak, nonatomic) NSManagedObjectContext *managedObjectContext;
and in the .m file i have:
在 .m 文件中,我有:
@synthesize managedObjectContext;
What this does (with syntehsiation) is make a setManagedObjectContext and getManagedObjectContext call. Upon being about to segue I check to make sure the destinationController will "respond to" this method, if so the data gets set.
这样做(使用合成)是进行 setManagedObjectContext 和 getManagedObjectContext 调用。在即将开始时,我检查以确保 destinationController 将“响应”此方法,如果是这样,则数据已设置。
clear?
清除?