Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

How to create the UIView and a UIViewController programatically without using a NIB

MoBillMoBill Posts: 3New Users
I am new to Iphone development. I am trying to learn some of the basics about creating views and view controllers. All the samples and documentation I've come across use the Interface Builder and NIB files to create UIViewControllers and UIViews. Is there a way to create the controller and the UIView programatically and not use the interface builder?

Thanks
Post edited by MoBill on

Replies

  • RickMaddyRickMaddy Posts: 2,122New Users
    edited November 2008
    Sure. My latest app has 32 view controllers and zero nibs.

    It's actually pretty easy once you get the basics down. Since you have to write the UIViewController based class either way, you are already half way there.

    Here's the pattern I use.
    1) Change your 'initWithNib' method with just and 'init' method. Set whatever you need in this 'constructor'.
    2) Don't use 'loadView'
    3) I use 'viewDidLoad' to create my entire view. Here I create all the items that will go in the view and add them as subviews to 'self.view'. You don't need to create the main view object. If you don't do this explicitly, an empty UIView will be created for you. This is what you get from 'self.view' - the empty view. So add your labels, buttons, images, whatever as subviews.
    4) Add 'viewWill/DidAppear' methods as needed.

    Of course without IB you now need to make all of the event connections yourself and calculate all of the subview frames but that gets easier and easier as you do this more.

    You end up with a lot more 'creation' code since just creating one UILabel can be 10 lines of code for all the little configuration items.

    But I like the control and I know what's going on. I don't like the 'magic' done for me.
  • backwardselvisbackwardselvis Posts: 30Registered Users
    edited November 2008
    RickMaddy wrote: »
    But I like the control and I know what's going on. I don't like the 'magic' done for me.

    Totally agree.
    Sure. My latest app has 32 view controllers and zero nibs.

    It's actually pretty easy once you get the basics down. Since you have to write the UIViewController based class either way, you are already half way there.

    Here's the pattern I use.
    1) Change your 'initWithNib' method with just and 'init' method. Set whatever you need in this 'constructor'.
    2) Don't use 'loadView'
    3) I use 'viewDidLoad' to create my entire view. Here I create all the items that will go in the view and add them as subviews to 'self.view'. You don't need to create the main view object. If you don't do this explicitly, an empty UIView will be created for you. This is what you get from 'self.view' - the empty view. So add your labels, buttons, images, whatever as subviews.
    4) Add 'viewWill/DidAppear' methods as needed.

    I personally use a 1:1 ratio.

    For every UIViewcontroller .h and .m file, I have a corresponding UIView .h and .m file to go with it.

    My View file is where I do most of my setup. If I need to grab any data from somewhere else my controller handles it.
  • SkylarECSkylarEC Posts: 74Registered Users
    edited November 2008
    Download my HelloRotation example from Index of /SampleCode to see what you need to do. I made it to show someone how to handle resizing a view while rotating, but it will work for you as will.
  • MoBillMoBill Posts: 3New Users
    edited November 2008
    Thank you all for your feedback. You have been very helpful. I am going to take your feedback and try to get it working. I also, don't like using gui designers that perform a bunch of hidden magic. That is exactly why i want to learn how to do everything programatically.

    SkylarEC,

    I must admit that i am also new to the Mac. I am having problems downloading your HelloRotation example. I was able to download your other examples that have the zip extension, but i am having problems with the Hello Rotation one. Is the file extension correct.

    Thanks
  • SkylarECSkylarEC Posts: 74Registered Users
    edited November 2008
    It is 7-zip. It compresses things to a much smaller size than .zip. If you don't have a 7-zip extractor, get one from here: 7-Zip
  • MoBillMoBill Posts: 3New Users
    edited November 2008
    I was able to download the example and extract it using the tool you recommended. Thanks for the info.

    I was also able to get my example working and gain a better understanding of the basics of views and viewcontrollers. Thanks all for your help.
  • brundlefly76brundlefly76 Posts: 15Registered Users
    edited November 2008
    RickMaddy wrote: »
    Sure. My latest app has 32 view controllers and zero nibs.

    What is the deal with interface builder anyway?

    It's nowhere near powerful enough to be a core application builder (like in VB), its not very good at anything but rudimentary design, but it isn't really doing new programmers much of a favor either - the sooner you learn to NOT use IB the better, IMHO.

    Maybe I'm just biased because I didn't even know it existed until after I was already working without it.
  • taqi.mirtaqi.mir Posts: 114Registered Users
    edited November 2008
    Hi,

    Even though i have followed your way to display the custom view ,but my view doesn't appear on the screen.

    - (void)viewDidLoad {
    [super viewDidLoad];
    [self displayAlertQuestion:@"Hello" withTitle:@"Bismillah"];
    NSString *_path=@"Images/woodbut.png";
    UIImage *img = [UIImage imageNamed:_path] ;
    _button =[[UIButton alloc]initWithFrame:CGRectMake(20,20,100,50)];
    [_button setBackgroundImage:img forState:0];
    [myView addSubview:_button];
    self.view=myView;

    }

    Any idea ?
  • taqi.mirtaqi.mir Posts: 114Registered Users
    edited November 2008
  • CommanderDataCommanderData Posts: 486Registered Users
    edited November 2008
    What is the deal with interface builder anyway?

    It's nowhere near powerful enough to be a core application builder (like in VB), its not very good at anything but rudimentary design, but it isn't really doing new programmers much of a favor either - the sooner you learn to NOT use IB the better, IMHO.

    Maybe I'm just biased because I didn't even know it existed until after I was already working without it.

    I'd agree, the old VB6 tools were better than interface builder is, and they are over 10 years old! VS.NET is light years ahead... I've been teaching myself to work without interface builder as well. Funny, before it was available in the SDK (early betas) I said I'd wait until it was available. Once it became available I vowed to use it as little as possible :D
    ChronoSoft - <a href="http://www.ChronoSoft.com" target="_blank">Rogue Touch</a> Available now in the <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=303471870&mt=8" target="_blank">App Store</a>! ... Version 1.5 available n
  • roocellroocell Posts: 98Registered Users
    edited February 2009
    RickMaddy, do you have any simple code example of this?
    I'm trying to do the same.
    - i have a NIB with a subview in it.
    - that subview is set to a custom class of mine.
    - in that subclass i'm trying to create another view

    But I cant seem to get that other view to actually appear.


    RickMaddy wrote: »
    Sure. My latest app has 32 view controllers and zero nibs.

    It's actually pretty easy once you get the basics down. Since you have to write the UIViewController based class either way, you are already half way there.

    Here's the pattern I use.
    1) Change your 'initWithNib' method with just and 'init' method. Set whatever you need in this 'constructor'.
    2) Don't use 'loadView'
    3) I use 'viewDidLoad' to create my entire view. Here I create all the items that will go in the view and add them as subviews to 'self.view'. You don't need to create the main view object. If you don't do this explicitly, an empty UIView will be created for you. This is what you get from 'self.view' - the empty view. So add your labels, buttons, images, whatever as subviews.
    4) Add 'viewWill/DidAppear' methods as needed.

    Of course without IB you now need to make all of the event connections yourself and calculate all of the subview frames but that gets easier and easier as you do this more.

    You end up with a lot more 'creation' code since just creating one UILabel can be 10 lines of code for all the little configuration items.

    But I like the control and I know what's going on. I don't like the 'magic' done for me.
  • RickMaddyRickMaddy Posts: 2,122New Users
    edited February 2009
    Several of the example apps from Apple don't use any nib files except for the main window such as UICatalog.
  • gchauraisgchaurais Posts: 1New Users
    edited March 2009
    RickMaddy wrote: »
    2) Don't use 'loadView'

    I don't know if I agree with that. The only reason for the loadView method to exist is to create views when you're not using a NIB.

    I would use the loadView method and assign my created view to self.view. This sounds more correct to me and it mimics the creation of a view with a NIB.

    If you use viewDidLoad to create your view (not just to set up stuff), you don't have control over what it's assigning to self.view (in this case, an empty view). In the future, if you want to move your code to another View.h /View.m file, you'll have to start using loadView anyways.

    I'd definitely recommend using loadView, because it sounds more compatible with UIViewController's design.

    It'll work the other way as well anyways.
  • nirisherenirishere Posts: 4New Users
    edited April 2011
    Well, I think that generating views programatically is just what I needed!

    Thing is, I want to set up a view so that the screen can be populated with many of them, and I want to format the view nicely in I.B.

    Is it possible to build a view in IB, and then in code call it many times with different parameters to generate different instances of the same view, having different images, text etc. ?

    I am quite new to the iPhone environment, Any piece of help will do.
    Thanks !
  • IngemarIngemar Posts: 15Registered Users
    edited June 2011
    SkylarEC wrote: »
    Download my HelloRotation example from Index of /SampleCode to see what you need to do. I made it to show someone how to handle resizing a view while rotating, but it will work for you as will.

    I have recently studied your example, which seems pretty good. It compiles and runs nicely.

    But I have run into a mystery. This must be something obvious for those of you who have been writing much ObjC, but I have not seen this mentioned in the two iPhone programming books I have.

    The UIView.beginAnimations: method does not exist in the headers, only beginAnimations:context:. And endAnimations doesn't seem to be declared at all! How is this possible? Is it legal to skip parameters, or is the demo using unofficial APIs?

    In the Mark&LaMarche book, they use beginAnimations:context: but no endAnimations:. Instead, they use commitAnimations.

    The demo also uses setContentView:, which seems to be defined for NSBox (whatever that is, never noticed before) but not for UIView in general.

    Again, the demo works just fine, but I am curious about these oddities.
  • baei1024baei1024 Posts: 47Registered Users
    edited June 2011
    RickMaddy wrote: »
    Sure. My latest app has 32 view controllers and zero nibs.

    It's actually pretty easy once you get the basics down. Since you have to write the UIViewController based class either way, you are already half way there.

    Here's the pattern I use.
    1) Change your 'initWithNib' method with just and 'init' method. Set whatever you need in this 'constructor'.
    2) Don't use 'loadView'
    3) I use 'viewDidLoad' to create my entire view. Here I create all the items that will go in the view and add them as subviews to 'self.view'. You don't need to create the main view object. If you don't do this explicitly, an empty UIView will be created for you. This is what you get from 'self.view' - the empty view. So add your labels, buttons, images, whatever as subviews.
    4) Add 'viewWill/DidAppear' methods as needed.

    Of course without IB you now need to make all of the event connections yourself and calculate all of the subview frames but that gets easier and easier as you do this more.

    You end up with a lot more 'creation' code since just creating one UILabel can be 10 lines of code for all the little configuration items.

    But I like the control and I know what's going on. I don't like the 'magic' done for me.

    Having a nib rather than code generating UI is better when there is UI designer who knows nothing about Objective-C in the team.

    You don't want every UI element position tuning comes to you, do you?

    Sometime programmatic is necessary.
Sign In or Register to comment.