43

I'm getting the following error from Xcode:

Couldn't compile connection: <IBCocoaTouchOutletConnection:0x401538380
<IBProxyObject: 0x40154a260> => categoryPicker => <IBUIPickerView: 0x4016de1e0>>

I've narrowed this down to a single outlet connection in storyboard. My code (about 30 views with lots of other connections) compiles and runs fine until I add a connection from a UIPicker to the view's categoryPicker property. The picker itself also works fine, I just can't reload it without getting this connection to work:

@interface FiltersTableViewController : UITableViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
    NSFetchedResultsController *fetchedResultsController;
    FilterTableViewController *filterView;

    AppDelegate *appDelegate;
    NSManagedObjectContext *managedObjectContext;       
}

@property (nonatomic, strong) FilterTableViewController *filterView;
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;

@property (nonatomic, weak) IBOutlet UIPickerView *categoryPicker;

- (void)configureCell:(FilterTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
- (void)performFetch;

@end

The UIPickerView is in a UITableViewCell. Here's an image of the storyboard, the connection from "categoryPicker" to "FiltersTableViewController" causes the error: enter image description here

Thanks for any ideas, or suggestions on how to debug it!

EDIT: I removed the connection and added one line to numberOfComponentsInPickerView:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    categoryPicker = pickerView;

    return 1;

}

This now works!, but I'd like to understand why the connection won't work and what that error message means. Right now this seems like a kludge to me since I use IB connections everywhere else to get object references.

EDIT 2: Connecting a prototype cell generates this error: Illegal Configuration: Connection "Cell" cannot have a prototype object as its destination. Not sure if this is new in Xcode 4.5.

Symmetric
  • 4,013
  • 3
  • 27
  • 33
  • Seems like the problem is, categoryPicker is "weak" and FilterTableViewController is "strong". Try to make categoryPicker strong as well and check if that works – TeaCupApp Feb 11 '12 at 00:23
  • 1
    Thanks. Just tried it, but it doesn't change anything. (I'm under the impression that IBOutlets should be weak...I get that from here: http://www.raywenderlich.com/5773/beginning-arc-in-ios-5-tutorial-part-2). – Symmetric Feb 11 '12 at 00:32
  • 1
    Yes thats technically right, sorry my bad! As I just read that in Apple documentation too! – TeaCupApp Feb 11 '12 at 00:33
  • so you just want to set your ViewController as a delegate of categoryPicker...am I right? have you tried categoryPicker.delegate = self; ? – TeaCupApp Feb 11 '12 at 00:37
  • The ViewController is already a delegate, and that part works. The problem is categoryPicker is nil without the connection from IB, and I can't call reloadAllComponents on it. I actually did just get something to work, but it seems like a kludge. I'll add it to the question in a sec... – Symmetric Feb 11 '12 at 00:45

2 Answers2

112

The problem is that this is a prototype cell. It is meaningless to have an outlet to something in it, because it isn't a real cell: it's a model for what might be dozens or hundreds of cells, and which one would the outlet point to in that case?

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 2
    Thank you! That makes sense now, the other places I use connections for this kind of thing use static cells. – Symmetric Feb 11 '12 at 01:25
  • matt, thanks for the answer. I'm stuck now. how do i address these objects like label button etc without a outlet? – carbonr Feb 28 '12 at 11:23
  • 7
    Use tags and call `viewWithTag:`, or subclass UITableViewCell so that each _cell_ has an outlet pointing to its _own_ button. – matt Feb 28 '12 at 14:11
  • @carbonr Also, you can save a reference from one of the delegate methods, like I did for categoryPicker above. Of course this only worked for me because there's only one picker instance. – Symmetric Mar 08 '12 at 22:10
  • 1
    I am able to create the connections on prototype cell without any problem? So is that still valid? – theiOSguy Aug 07 '13 at 17:13
  • @theiOSguy I think it is as I just ran into this problem. – Bot Aug 27 '13 at 22:01
  • 1
    Well you could use the tag of the button to recognize the cell. An outlet from a cell prototype should be allowed and would be possible. – bio Apr 14 '16 at 15:00
23

SWIFT 2

I was creating a popover segue and I was getting the same error.

What I did was follow @matt's answer by not putting it on a cell, which is logical now that he explained it!

Instead, I put the TableView as the anchor and it worked fine.

Hope that helps those in the future.

Lukesivi
  • 2,206
  • 4
  • 25
  • 43
  • 3
    Thanks for adding this, i had this problem on swift 2 and i didn't know why – kevinrodriguez-io Dec 02 '15 at 20:49
  • 2
    Glad it helped! @darkndream – Lukesivi Dec 03 '15 at 07:35
  • @PaulBrewczynski I just added a popover segue + TableView as an Anchor in the Storyboard as explained above. – Lukesivi Jan 05 '17 at 14:35
  • Note that you can then change the anchor to point to the correct cell during prepareforSegue to the actual cell. (pass cell as sender in performSegue; then segue.destinationViewController.popoverPresentationController.sourceView = (UIView *) sender – mackworth Sep 18 '20 at 00:09