I am trying to assign a block to a button object so that each time a button is pressed the block will execute. I have the following defined button subclass that holds the block for each unique button I create.
typedef void (^ButtonPressBlock)();
@interface PhotoButton : UIButton
{
ButtonPressBlock photoButtonPressed;
}
@property (copy) ButtonPressBlock photoButtonPressed;
@end
There is a @synthesize photoButtonPressed in the .m
In a separate UIViewController I #import “PhotoButton.h” and then in that view controller I have a method that creates the buttons. The code looks like this.
PhotoButton* photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
photoButton.frame = CGRectMake(i, j, thumbSize+2, thumbSize+2);
[photoButton setImage:photoThumb forState:UIControlStateNormal];
[photoButton setAdjustsImageWhenHighlighted:NO];
[photoButton setPhotoButtonPressed:^()
{
[self performSegueWithIdentifier:@"photoSegue" sender:aPhoto];
}];
assigning the block to the photoButton fails at execution with a
-[UIButton setPhotoButtonPressed:]: unrecognized selector sent to instance…
Not sure what I am doing wrong as blocks are new to me. I thought this would be a great way to make a thumbnail image responsive to a touch and then segue to the full sized image in another view controller. Now I’m not sure.
This:
PhotoButton* photoButton = [UIButton buttonWithType:UIButtonTypeCustom];should be:
PhotoButton* photoButton = [PhotoButton buttonWithType:UIButtonTypeCustom];You were creating an instance of
UIButton, notPhotoButton