按下UI按钮时出现内存错误。我在自定义类文件中动态创建这些按钮,因此每次调用类时,都会根据视图控制器创建一定数量的按钮。他们创建的代码如下:
- (void)addTopicButtonsWithCount:(int)count andView:(UIScrollView*)referenceView
{
int i = 0;
for(NSString *theory in _chapterTheory)
{
[self makeButtonWithTitle:[NSString stringWithFormat:@"%@", self.chapterTheory[i][@"title"]] atPositionIndex:i andView:referenceView];
i++;
}
}
- (void)makeButtonWithTitle:(NSString *)title atPositionIndex:(NSInteger)index andView: (UIScrollView*)referenceView
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(80, 80+(60*index), 160, 40);
button.tag = index;
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:16];
button.titleLabel.textColor = [UIColor blackColor];
button.backgroundColor = [UIColor blackColor];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[referenceView addSubview:button];
}
创建按钮后,我尝试使用此功能访问它们:
- (void)buttonPressed:(UIButton *)sender {
NSLog(@"button %d pressed", sender.tag+1);
}
但是我甚至没有达到这个功能,我在第一行这个buttonPressed方法上设置了一个断点,但是编译器永远不会达到此品脱,它会事先崩溃。按钮好像很好,我错过了什么?
请求崩溃日志:
2014-02-05 21:27:49.242 theoryDisplay [67820:70b] - [UIScrollView buttonPressed:]:无法识别的选择器发送到实例0x8d54180
2014-02-05 21:27:49.245 theoryDisplay [67820:70b] * 由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [ UIScrollView buttonPressed:]:无法识别的选择器发送到实例0x8d54180'
* 第一次抛出调用堆栈: ( 0 CoreFoundation 0x0173b5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014be8b6 objc_exception_throw + 44
2 CoreFoundation 0x017d8903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0172b90b ___forwarding___ + 1019
4 CoreFoundation 0x0172b4ee _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x014d0874 -[NSObject performSelector:withObject:withObject:] + 77
6 UIKit 0x0022e0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x0022e04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x003260c1 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x00326484 -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x00325733 -[UIControl touchesEnded:withEvent:] + 641
11 UIKit 0x005a0c7f _UIGestureRecognizerUpdate + 7166
12 UIKit 0x0026b19a -[UIWindow _sendGesturesForEvent:] + 1291
13 UIKit 0x0026c0ba -[UIWindow sendEvent:] + 1030
14 UIKit 0x0023fe86 -[UIApplication sendEvent:] + 242
15 UIKit 0x0022a18f _UIApplicationHandleEventQueue + 11421
16 CoreFoundation 0x016c483f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
17 CoreFoundation 0x016c41cb __CFRunLoopDoSources0 + 235
18 CoreFoundation 0x016e129e __CFRunLoopRun + 910
19 CoreFoundation 0x016e0ac3 CFRunLoopRunSpecific + 467
20 CoreFoundation 0x016e08db CFRunLoopRunInMode + 123
21 GraphicsServices 0x036e09e2 GSEventRunModal + 192
22 GraphicsServices 0x036e0809 GSEventRun + 104
23 UIKit 0x0022cd3b UIApplicationMain + 1225
24 theoryDisplay 0x0000371d main + 141
25 libdyld.dylib 0x01d7970d start + 1
) libc ++ abi.dylib:以NSException类型的未捕获异常终止