1
votes

I have a view controller configured with a UICollectionView which has a prototype cell defined and working perfectly. I have a segue set up so that if you tap on the prototype cell, it will flip to a "schedule" view for that particular cell. This also works great.

However I now want to add a button to the prototype cell (or a switch) that will use a different segue to go to a different view controller (settings) for whichever cell you tap that button on.

I've created the button, ctrl-clicked and dragged to the View Controller I want to go to, the segue is created and all looks fine. But when I run the app and tap on the button (no matter which cell) it gives the error:

-[UIButton _layoutAttributes]: unrecognized selector sent to instance

I can't figure out what I've done wrong. Is there anything obvious from the above description? I can post code if necessary, but I don't see how it will help in this case as everything is done on the storyboard with regard to the segues etc. There's no code yet on the "Settings" view controller.

The error appears to occur as soon as you tap the button and before prepareForSegue is even called.

1
Post the complete crash log. - Droppy

1 Answers

1
votes

You have a prototype cell with a button and you are using a segue from that button which is wrong way AFAIK. Reason I say this is prototype cell is not actual cell but a template to create instances of cell during execution.

This should be as follow:

  1. Define Prototype cell with UIButton
  2. Attach a segue from SourceController to DestinationController(neither from cell nor button)
  3. In -collectionView:cellForItemAtIndexPath: add -addTarget:action:forControlEvents: for particular button
  4. Handle whatever - performSegueWithIdentifier:sender: in action method specified in above step.

Edit: -addTarget:action

Declare a button in UICollectionViewCell subclass and connect an outlet to the UIButton in the prototype cell.

@interface <your custom class here> : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIButton *buttonOnCell; //<-- Connect outlet to prototype cell.

@end

In -collectionView:cellForItemAtIndexPath: after dequeue cell add the following line:

[cell.buttonOnCell addTarget:self action:@selector(actionOnButton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;

Define a method in your viewController as follows:

- (void)actionOnButton:(UIButton*)button
{
    // your code here depending on the button pressed
}