0

Im curious, where is the best option to allocate/init, set attributes of views (uibutton, uilabel, uitextfield, initializing variables, etc).

This is in regards to developing an app strictly programatically. I see some cases where these views have been allocated/init in the class -init method, but then other times i see other views set in the -loadview method.

Can anyone provide some clarity about this? And maybe some abstract examples of when the best time to do it for either method would be.

Thanks

s_dev
  • 245
  • 5
  • 17

2 Answers2

-1

Here's a quick comment on this:

-SubClass a UIView, smash all your UI elements into that view, well as many as you can at least. Import this subclassed view's header into your view controller's implementation file

-In your view controller, typecast your view controller's view like so:

-(HHYSignUpViewFirstPhase*)contentView
{
    return (id)[self view];
}

-Invoke the loadView method

-(void)loadView
{
    [self setView:[HHYSignUpViewFirstPhase new]];
}

-In your viewdidLoad, you can now set handlers to buttons and such from your subclassed UIView by calling to "[self contentView]" like so:

  -(void)viewDidLoad
    {
        [super viewDidLoad];
        [self setTitles:@"Sign Up"];
        [[[self contentView] nameField] setDelegate:self];
        [[[self contentView] emailField] setDelegate:self];
        [[[self contentView] passwordField] setDelegate:self];
        [[[self contentView] signupButton] addTarget:self action:@selector(signupPressed) forControlEvents:UIControlEventTouchUpInside];
    }

Now you have it all set up, you just need to add methods to handle events from the button, for example in the view did load from your subview that you subclassed:

-(void)signupPressed
{
   ///do work
}

UIVIew subclass:

HHYSignUpViewFirstPhase.h

@interface HHYSignUpViewFirstPhase : UIView

@property (nonatomic) UIButton * signupButton;
@property (nonatomic) UITextField * emailField;
@property (nonatomic) UITextField * nameField;
@property (nonatomic) UITextField * passwordField;

@end

HHYSignUpViewFirstPhase.m

#import "HHYSignUpViewFirstPhase.h"

@implementation HHYSignUpViewFirstPhase

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self == nil)
        return nil;


          //do work, set up buttons, constraints, etc, etc.


         return self;
}
@end

Essentially, what I'm saying here is that in the subclassed UIView you can initialize the UIView and set up all its constraints and EVERYTHING, frames included and then in the load view method of your UIViewController, you then call to this view and typcast the view of the UIViewController. So, sometimes you do the set up in the init, sometimes you do it in the load view, it depends on what you are trying to do, but this is how you set this up in a pure programmatic fashion with separation of duties, encapsulation, and all tied together in an MVC framework -- all work is separated into classes, and all controllers control a single class.

http://matthewmorey.com/creating-uiviews-programmatically-with-auto-layout/

and this

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html#//apple_ref/doc/uid/TP40007457-CH10-SW36

Larry Pickles
  • 4,615
  • 2
  • 19
  • 36
  • How does this answer the question? – Ewan Mellor Aug 17 '15 at 23:21
  • it shows him how to set it up programmatically, did you read the question? – Larry Pickles Aug 17 '15 at 23:22
  • and to reenforce what I'm saying, here you go, see the link at the bottom of my answer – Larry Pickles Aug 17 '15 at 23:36
  • When is it ever ok to create a view programmatically in the -init method, instead of the -loadview? Such as a button, or label. Ive seen it done, just not sure if there is a good reason for it, or if its just bad code. – s_dev Aug 18 '15 at 14:41
  • lol, you are kidding right? this is how you program UIView subclasses – Larry Pickles Aug 18 '15 at 14:43
  • And, furthermore, this would BE INSIDE A UIVIEW SUBCLASS NOT THE UIVIEWCONTROLLER – Larry Pickles Aug 18 '15 at 14:44
  • reread the answer, THIS is a UIVIEW: HHYSignUpViewFirstPhase.h – Larry Pickles Aug 18 '15 at 14:45
  • I love it, you guys here on SO are unbelievable, I don't even know why I waste my time posting here, you just don't understand the way things work from a programmatic, purist coding methodology. It's okay, keep working with nibs. One day photoshop will have a plugin that auto generates an app for you and then you will be all set up. – Larry Pickles Aug 18 '15 at 14:47
  • You do realize that the UIView subclass is being build in the LoadView don't you? this is a separate file, this means you have this: HHYSignUpViewFirstPhase.h, HHYSignUpViewFirstPhase.m AND you have HHYSignUpViewFirstPhaseController.h AND this, HHYSignUpViewFirstPhaseController.m , that's 4 files, not one file, you asked to do things programmatically, and this is how you do it, I have some View controllers that require around 40 files, that's how sub classing works and that's how programmatic view controllers, work, that's it, I'm done explaning this to you – Larry Pickles Aug 18 '15 at 14:52
  • sorry man. I wasn't implying anything. Im actually just asking a question, because i don't know the answer. I wasn't implying anything about your code, just asking. I want to KNOW the correct way. These were just general questions. My last comment, had nothing to do with your code. Currently, the product that I'm working on, which was already built, is not using a separate UIVIEW, but doing the view inside the view controller. There are some views(textfields) that are being init/allocated in the -init method, and then some that are being allocat/init from the -loadView method. – s_dev Aug 19 '15 at 19:29
  • All i want to know, is if this is correct practice or not. If it is, when is it ok to allocate/init within the -loadView or when is it ok to allocate/init views(textfields, buttons, labels) inside the view controllers -init method. I don't know the answer, and i want to know whats correct. – s_dev Aug 19 '15 at 19:30
-1

The -init* family of functions would be a good place to initialize simple properties, e.g. strings, numbers, and the like. The initializer runs just after the memory for the object is allocated, and if you have something that can be initialized there then you should do it there.

For UIViewController instances, you probably have to wait until the nib has been loaded before you can initialize everything else. If you've got images that need to be placed inside subviews, or fonts that need configuring, or whatever, then you need to have the nib loaded first. -viewDidLoad is the best place for that stuff.

For UIView instances (or subclasses like UITableViewCell), you need to wait for the nib to be loaded too. You can use -awakeFromNib in that case.

Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39
  • 1
    he said programmatic, not with a Nib – Larry Pickles Aug 17 '15 at 23:22
  • no nibs, nibs are for those who don't know how to program, thanks, and he asked for an abstract example, didn't he? shoudl you have any additional questions, feel free to read here: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutinCode/AutoLayoutinCode.html – Larry Pickles Aug 17 '15 at 23:23
  • Oh yeah, and I almost forgot to tell you why I down voted you, read the comments above, and the comments below my answer. thanks – Larry Pickles Aug 17 '15 at 23:52