EXE BAD ACCESS Error from touching UIbutton which was dynamically created in a class

0

I am getting a memory error when I press my UIbuttons. I am creating these buttons dynamically in a custom class file, so everytime the class is called a certain number of buttons are created depending on the view controller. The code for their creation is as follows:

- (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];
}

Once the buttons are created I am trying to access them with this function:

- (void)buttonPressed:(UIButton *)sender {
    NSLog(@"button %d pressed", sender.tag+1);
}

But I don't even get to this function, I've placed a break point on the first line this buttonPressed method, but the compiler never reaches this pint, it crashes beforehand. The buttons seem to be created fine, what have I missed?

Crash Log as requested:

2014-02-05 21:27:49.242 theoryDisplay[67820:70b] -[UIScrollView buttonPressed:]: unrecognized selector sent to instance 0x8d54180

2014-02-05 21:27:49.245 theoryDisplay[67820:70b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIScrollView buttonPressed:]: unrecognized selector sent to instance 0x8d54180'

* First throw call stack: ( 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: terminating with uncaught exception of type NSException

ios
objective-c
ios7
uibutton
asked on Stack Overflow Feb 4, 2014 by kaf8963 • edited Feb 5, 2014 by kaf8963

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0