0

I have created a custom view and added a UITextField to it.My question is: How can I add a delegate so that the you can set it as the default cmd+drag behaviour of XCode?

My current attempt is by doing something like this:

In my .h file:

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface CustomTextField : UIView

   @property (assign, nonatomic) IBInspectable id textFieldDelegate;

@end

and my .m file:

#import "CustomTextField.h"

@interface CustomTextField()

@property (strong, nonatomic) IBOutlet UITextField *textField;

@end

@implementation CustomTextField

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self loadNib];
    }
    return self;
}

- (void)loadNib{

    UIView *view = [[[NSBundle bundleForClass:[self class]] loadNibNamed:@"CustomTextField" owner:self options:nil] firstObject];
    [self addSubview:view];
    view.frame = self.bounds;

}

- (void)setTextFieldDelegate:(id)textFieldDelegate{
    self.textField.delegate = textFieldDelegate;
}

@end

But it doest show up in the left panel of XCode, Connections Inspector Tab.

Also here is my current code.

Update 1:

also if using the code below I get a an error:

-  (id)initWithFrame:(CGRect)aRect
{
    self = [super initWithFrame:aRect];

    if (self)
    {
        [self loadNib];
    }

    return self;
}

- (id)initWithCoder:(NSCoder*)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self loadNib];
    }

    return self;
}


- (void)loadNib{
//
//    UIView *view = [[[NSBundle bundleForClass:[self class]] loadNibNamed:@"CustomTextField" owner:self options:nil] firstObject];
//    [self addSubview:view];
//    view.frame = self.bounds;

    UIView *view = [[[NSBundle bundleForClass:[self class]] loadNibNamed:@"CustomTextField" owner:self options:nil] firstObject];
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self addSubview:view];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|­" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|­" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];

    self.textLabel.text = @"1";

}

Any suggestions on what should I do?

Update 2 I ended up by replacing the initWithFrame, initWithCoder and loadNib functions with:

- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder
{
    if (![self.subviews count])
    {
        NSBundle *mainBundle = [NSBundle mainBundle];
        NSArray *loadedViews = [mainBundle loadNibNamed:@"CustomTextField" owner:nil options:nil];
        CustomTextField *loadedView = [loadedViews firstObject];

        loadedView.frame = self.frame;
        loadedView.autoresizingMask = self.autoresizingMask;
        loadedView.translatesAutoresizingMaskIntoConstraints =
        self.translatesAutoresizingMaskIntoConstraints;

        for (NSLayoutConstraint *constraint in self.constraints)
        {
            id firstItem = constraint.firstItem;
            if (firstItem == self)
            {
                firstItem = loadedView;
            }
            id secondItem = constraint.secondItem;
            if (secondItem == self)
            {
                secondItem = loadedView;
            }
            [loadedView addConstraint:
             [NSLayoutConstraint constraintWithItem:firstItem
                                          attribute:constraint.firstAttribute
                                          relatedBy:constraint.relation
                                             toItem:secondItem
                                          attribute:constraint.secondAttribute
                                         multiplier:constraint.multiplier
                                           constant:constraint.constant]];
        }

        return loadedView;
    }
    return self;
}
Laur Stefan
  • 1,519
  • 5
  • 23
  • 50
  • Why is the delegate declared as IBInspectable ? Also you are not using the delegate created! – Teja Nandamuri Jun 22 '16 at 17:26
  • Any examples? just started working with `IB_DESIGNABLE` and `IBInspectable` so not sure yet on how to use them, I will update the question with the actual code files – Laur Stefan Jun 22 '16 at 17:32
  • Not all types are capable of being IBInspectable. Only, "booleans, strings, and numbers (i.e., NSNumber or any of the numeric value types), as well as CGPoint, CGSize, CGRect, UIColor, and NSRange, adding UIImage for good measure." For more info see [here](http://nshipster.com/ibinspectable-ibdesignable/) – beyowulf Jun 22 '16 at 17:45
  • You don't have to create the delegate unless you are doing custom actions in your customView. You can always use the textField delegate and connect it your customView file owner. The custom view file owner should be your view controller where the custom view is presented in! – Teja Nandamuri Jun 22 '16 at 17:47

0 Answers0