Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Fritzables

macrumors regular
Original poster
May 6, 2011
149
1
Brisbane AUSTRALIA
Hi All,

I am going to give, making my own Controls and see how successful things turn out.

The one I am creating now is pretty simple, it's a UIView that has a Label, Text Field & Button.

I have done this by making a XIB file with the associated Header and Implementation files.

At this point, I have subclassed it as a UIView and the following illustrates how I have done this:

NameView.h
Code:
#import <UIKit/UIKit.h>

@interface NameView : UIView
@property (strong, nonatomic) IBOutlet UIView *view;
@end

NameView.m
Code:
#import "NameView.h"

@implementation NameView
@synthesize view = _view;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        [self setup];
    }
    
    return self;
}
- (void)setup {
    [[NSBundle mainBundle] loadNibNamed:@"NameView" owner:self options:nil];
    [self addSubview:self.view];
}
@end


In the applications code, I am calling the control with:
Code:
NameView *nameView = [[NameView alloc] initWithFrame:CGRectFrame(20.0, 75.0, 633.0, 124.0)];
[self.view addSubview:nameView];

When running the application, I do not see errors nor do I see the expected NameView control appear.

I am now wondering if I have subclassed incorrectly. Should I be using UIControl instead of UIView in the code for the control, or have I make a mistake elsewhere??

Pete
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
Hi All,

I am going to give, making my own Controls and see how successful things turn out.

The one I am creating now is pretty simple, it's a UIView that has a Label, Text Field & Button.

I have done this by making a XIB file with the associated Header and Implementation files.

At this point, I have subclassed it as a UIView and the following illustrates how I have done this:

NameView.h
Code:
#import <UIKit/UIKit.h>

@interface NameView : UIView
@property (strong, nonatomic) IBOutlet UIView *view;
@end

NameView.m
Code:
#import "NameView.h"

@implementation NameView
@synthesize view = _view;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        [self setup];
    }
    
    return self;
}
- (void)setup {
    [[NSBundle mainBundle] loadNibNamed:@"NameView" owner:self options:nil];
    [self addSubview:self.view];
}
@end


In the applications code, I am calling the control with:
Code:
NameView *nameView = [[NameView alloc] initWithFrame:CGRectFrame(20.0, 75.0, 633.0, 124.0)];
[self.view addSubview:nameView];

When running the application, I do not see errors nor do I see the expected NameView control appear.

I am now wondering if I have subclassed incorrectly. Should I be using UIControl instead of UIView in the code for the control, or have I make a mistake elsewhere??

Pete

Your initWithCoder method will only be called if you invoke your custom control from a nib file. IF you create one of your controls through code, initWithFrame will be called instead.

I generally write an initSetup method, and call it from both initWithCoder and the object's designated initializer.

In your case, you already have a setup method. Simply add a call to your setup method to your initWithFrame method, since you already call it from initWithCoder.
 

Fritzables

macrumors regular
Original poster
May 6, 2011
149
1
Brisbane AUSTRALIA
G'Day dejo,

No, it dosen't (ggod pickup).

But, if I was to place a UIView (via IB) onto the applications main view and then have the Class for the UIView reference the NameView then it works fine.

So, when I have it is currently, it's not calling setup - is this just a matter of:

Code:
NameView *nameView = [[NameView alloc] initWithFrame:CGRectFrame(20.0, 75.0, 633.0, 124.0)];
[self.view addSubview:nameView.setup];

Pete
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.