Search code examples
iosxcode6protocolsxibiboutlet

Cannot connect IBOutlet of UIView declared in protocol to a xib entity


I have this IBOutlet declared in protocol:

@protocol SomeProtocol <NSObject>

@property (nonatomic, strong) IBOutlet UIView* view;


@end

Then there is a class which inherits from this protocol:

//.h
@interface SomeClass : NSObject <SomeProtocol>
@end

//.m
@implementation TSNFactoryViews
@synthesize view = _view;
@ned

And in a xib file I set SomeClass as class owner so that I expect I could see the view outlet and connect it to an appropriate xib instance.

In XCode 4 I had no issue with this concept. But since XCode 5, the view disappears sometime and in XCode 6 it has recently disappeared, but strangely it has disappeared in all xib files, but the application can compile and run without issue.

The only problem I have is with the new xib files because I can not setup the relationship. It looks like a bug to me.

EDIT:

The SomeClass in my case is a factory class which generates view instances or/and keeps instances of view. I do it this way for several years without issue. The connection bullet on the left from IBOutlet UIView view;* just disappeared. I have many xib files where the file owners are set to the single factory class (in this post as SomeClass) generating custom views.

enter image description here


Solution

  • A protocol only lays out a blueprint as to what needs to be in a class. Declaring the IBOutlet in the protocol does not magically add this property to any class that adheres to the protocol.

    You have to add the IBOutlet UIView* to the interface of SomeClass, then you will be able to connect it.

    Additionally, it makes no sense at all to declare an IBOutlet in a protocol. A protocol should declare an interface or API - the means of implementation should be up to the class adhering to a protocol. Therefore it is questionable to declare properties at all, but you should rather declare methods (getters or setters).

    In your case that means, that you would add this to your protocol:

    - (UIView*)view;
    

    and your class SomeClass can choose to implement this via a property like this:

    @property (nonatomic, strong) IBOutlet UIView* view;