Building iOS Controls ?

Discussion in 'iOS Programming' started by Fritzables, Nov 12, 2013.

  1. Fritzables macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #1
    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
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Have you verified that your setup method is getting called?
     
  3. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    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.
     
  4. Fritzables thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #4
    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
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    As Duncan alluded to, your initWithCoder: calls [self setup]; but your initWithFrame: doesn't.
     
  6. Fritzables thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA

Share This Page