我一直在编程应用程序而不使用故事板。
因此我相信这个“无法识别的选择器发送到实例”错误与按钮和其他对象之间的某些断开连接无关。
在我的主页上,我正在加载另一个类“YourPicksVC”的视图
-(void)loadYourPicks{
yourPickView1 = [[UIView alloc]init];
YourPicksVC *ypVC = [[YourPicksVC alloc]init];
[ypVC initYourPicks];
yourPickView1 = ypVC.yourPicksView;
[pageDisplay addSubview:yourPickView1];
}
在类yourPicksVC中,我有一个函数“initYourPicks”
restaurants = [UIButton buttonWithType:UIButtonTypeCustom];
[restaurants setTitle:@"Restaurants" forState:UIControlStateNormal];
[restaurants setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
restaurants.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
restaurants.titleLabel.textAlignment = NSTextAlignmentLeft;
[restaurants setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[restaurants addTarget:self action:@selector(restaurantCategories) forControlEvents:UIControlEventTouchUpInside];
restaurants.frame = CGRectMake(20, 70, 100, 50);
[yourPicksView addSubview:restaurants];
因为我正在调试我的代码,所以我只需要
-(void)restaurantCategories{
NSLog(@"very nice");
restaurantsView = [[UIView alloc]initWithFrame:CGRectMake(120, 568, 100, 518)];
[yourPicksView addSubview:restaurantsView];
}
这是我收到的消息:
2014-05-20 18:33:59.037 Ally[19839:70b] -[__NSCFType restaurantCategories]: unrecognized selector sent to instance 0x94bfec0
2014-05-20 18:33:59.039 Ally[19839:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType restaurantCategories]: unrecognized selector sent to instance 0x94bfec0'
*** First throw call stack:
(
0 CoreFoundation 0x027615e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01cb08b6 objc_exception_throw + 44
2 CoreFoundation 0x027fe903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0275190b ___forwarding___ + 1019
4 CoreFoundation 0x027514ee _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x01cc2874 -[NSObject performSelector:withObject:withObject:] + 77
6 UIKit 0x00a200c2 -[UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x00a2004e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x00b180c1 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x00b18484 -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x00b17733 -[UIControl touchesEnded:withEvent:] + 641
11 UIKit 0x00a5d51d -[UIWindow _sendTouchesForEvent:] + 852
12 UIKit 0x00a5e184 -[UIWindow sendEvent:] + 1232
13 UIKit 0x00a31e86 -[UIApplication sendEvent:] + 242
14 UIKit 0x00a1c18f _UIApplicationHandleEventQueue + 11421
15 CoreFoundation 0x026ea83f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
16 CoreFoundation 0x026ea1cb __CFRunLoopDoSources0 + 235
17 CoreFoundation 0x0270729e __CFRunLoopRun + 910
18 CoreFoundation 0x02706ac3 CFRunLoopRunSpecific + 467
19 CoreFoundation 0x027068db CFRunLoopRunInMode + 123
20 GraphicsServices 0x035b89e2 GSEventRunModal + 192
21 GraphicsServices 0x035b8809 GSEventRun + 104
22 UIKit 0x00a1ed3b UIApplicationMain + 1225
23 Ally 0x0000c24d main + 141
24 libdyld.dylib 0x02ca2701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
请让我知道我做错了什么。我在其他应用程序中尝试过类似的方法,但它运行正常。
-(void)loadYourPicks1{
result = [[UIScreen mainScreen] bounds].size;
yourPicksView = [[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, result.height-50)];
[yourPicksView setBackgroundColor:[UIColor redColor]];
restaurants = [UIButton buttonWithType:UIButtonTypeCustom];
[restaurants setTitle:@"Restaurants" forState:UIControlStateNormal];
[restaurants setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
restaurants.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
restaurants.titleLabel.textAlignment = NSTextAlignmentLeft;
[restaurants setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[restaurants addTarget:self action:@selector(restaurantCategories) forControlEvents:UIControlEventTouchUpInside];
restaurants.frame = CGRectMake(20, 70, 100, 50);
[yourPicksView addSubview:restaurants];
//TASK: Each time we click on one of the buttons, more detailed categories will show up!
bars = [UIButton buttonWithType:UIButtonTypeCustom];
[bars setTitle:@"Bars" forState:UIControlStateNormal];
[bars setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
bars.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
bars.titleLabel.textAlignment = NSTextAlignmentLeft;
[bars setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
bars.enabled = YES;
[bars addTarget:self action:@selector(barCategories) forControlEvents:UIControlEventTouchUpInside];
bars.frame = CGRectMake(20, 215, 100, 50);
hairSalon = [UIButton buttonWithType:UIButtonTypeCustom];
[hairSalon setTitle:@"Hair Salons" forState:UIControlStateNormal];
[hairSalon setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
hairSalon.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
hairSalon.titleLabel.textAlignment = NSTextAlignmentLeft;
[hairSalon setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
hairSalon.enabled = YES;
[hairSalon addTarget:self action:@selector(lol) forControlEvents:UIControlEventTouchUpInside];
hairSalon.frame = CGRectMake(20, 145, 100, 50);
nailSpa = [UIButton buttonWithType:UIButtonTypeCustom];
[nailSpa setTitle:@"Nail Spas" forState:UIControlStateNormal];
[nailSpa setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
nailSpa.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
nailSpa.titleLabel.textAlignment = NSTextAlignmentLeft;
[nailSpa setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
nailSpa.enabled = YES;
nailSpa.frame = CGRectMake(20, 285, 100, 50);
health = [UIButton buttonWithType:UIButtonTypeCustom];
[health setTitle:@"Health" forState:UIControlStateNormal];
[health setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
health.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
health.titleLabel.textAlignment = NSTextAlignmentLeft;
[health setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
health.enabled = YES;
health.frame = CGRectMake(20, 355, 100, 50);
massage = [UIButton buttonWithType:UIButtonTypeCustom];
[massage setTitle:@"Massage" forState:UIControlStateNormal];
[massage setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
massage.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
massage.titleLabel.textAlignment = NSTextAlignmentLeft;
[massage setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
massage.enabled = YES;
massage.frame = CGRectMake(20, 425, 100, 50);
[yourPicksView addSubview:bars];
[yourPicksView addSubview:coffeeShops];
[yourPicksView addSubview:nailSpa];
[yourPicksView addSubview:hairSalon];
[yourPicksView addSubview:health];
[yourPicksView addSubview:massage];
}
答案 0 :(得分:0)
你的问题就在这里 -
-(void)loadYourPicks{
yourPickView1 = [[UIView alloc]init];
YourPicksVC *ypVC = [[YourPicksVC alloc]init];
[ypVC initYourPicks];
yourPickView1 = ypVC.yourPicksView;
[pageDisplay addSubview:yourPickView1];
}
您在YourPicksVC
方法中将loadYourPicks
分配为本地变量。您要将UIView
中的YourPicksVC
作为子视图添加到pageDisplay
,因此pageDisplay
会强制引用UIView
并阻止其成为YourPicksVC
释放。遗憾的是,没有任何内容对loadYourPicks
本身有强烈的引用,因此当YourPicksVC
返回时,restaurantCategories
对象将被取消分配。一段时间后,您触摸按钮并调用YourPicksVC
选择器,但过去包含__NSCFType
的内存已被释放并重新分配给另一个对象 - 在本例中为restaurantCategories
,它不理解YourPicksVC
选择器,因此你得到一个例外。
如果您创建了一个属性来保存@property (strong,nonatomic) YourPicksVC *ypVC;
-(void)loadYourPicks{
yourPickView1 = [[UIView alloc]init];
self.ypVC = [[YourPicksVC alloc]init];
[self.ypVC initYourPicks];
yourPickView1 = self.ypVC.yourPicksView;
[pageDisplay addSubview:yourPickView1];
}
并在您的代码中使用它,然后视图控制器将不会被解除分配 -
{{1}}
如果您搜索过“__NSCFType无法识别的选择器”,您会发现它通常是由对已解除分配的对象的无效引用引起的,这是HotLicks试图为您提供的提示。