Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-06-2008, 08:17 PM   #1 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 3
MoBill is on a distinguished road
Default How to create the UIView and a UIViewController programatically without using a NIB

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

Last edited by MoBill; 11-06-2008 at 08:20 PM.
MoBill is offline   Reply With Quote
Old 11-06-2008, 09:08 PM   #2 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
RickMaddy will become famous soon enough
Default

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.
RickMaddy is offline   Reply With Quote
Old 11-06-2008, 09:38 PM   #3 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 30
backwardselvis is on a distinguished road
Default

Quote:
Originally Posted by RickMaddy View Post
But I like the control and I know what's going on. I don't like the 'magic' done for me.
Totally agree.

Quote:
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.
backwardselvis is offline   Reply With Quote
Old 11-07-2008, 07:00 AM   #4 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 74
SkylarEC is on a distinguished road
Default

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.
SkylarEC is offline   Reply With Quote
Old 11-07-2008, 08:02 PM   #5 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 3
MoBill is on a distinguished road
Default

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
MoBill is offline   Reply With Quote
Old 11-07-2008, 08:18 PM   #6 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 74
SkylarEC is on a distinguished road
Default

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
SkylarEC is offline   Reply With Quote
Old 11-07-2008, 10:07 PM   #7 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 3
MoBill is on a distinguished road
Default

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.
MoBill is offline   Reply With Quote
Old 11-07-2008, 11:52 PM   #8 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 15
brundlefly76 is on a distinguished road
Default

Quote:
Originally Posted by RickMaddy View Post
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.
brundlefly76 is offline   Reply With Quote
Old 11-16-2008, 06:52 AM   #9 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 114
taqi.mir is an unknown quantity at this point
Default

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.mir is offline   Reply With Quote
Old 11-16-2008, 06:53 AM   #10 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 114
taqi.mir is an unknown quantity at this point
Default

Any Idea ?
taqi.mir is offline   Reply With Quote
Old 11-16-2008, 08:36 AM   #11 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 486
CommanderData is on a distinguished road
Default

Quote:
Originally Posted by brundlefly76 View Post
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
CommanderData is offline   Reply With Quote
Old 02-27-2009, 09:48 PM   #12 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 97
roocell is on a distinguished road
Default

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.



Quote:
Originally Posted by RickMaddy View Post
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.
roocell is offline   Reply With Quote
Old 02-27-2009, 10:46 PM   #13 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
RickMaddy will become famous soon enough
Default

Several of the example apps from Apple don't use any nib files except for the main window such as UICatalog.
RickMaddy is offline   Reply With Quote
Old 03-17-2009, 02:22 PM   #14 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 1
gchaurais is on a distinguished road
Default

Quote:
Originally Posted by RickMaddy View Post
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.
gchaurais is offline   Reply With Quote
Old 04-28-2011, 05:49 PM   #15 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 4
nirishere is on a distinguished road
Default Great thread, I need some help here pls

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 !
nirishere is offline   Reply With Quote
Old 06-08-2011, 02:34 AM   #16 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 15
Ingemar is on a distinguished road
Default

Quote:
Originally Posted by SkylarEC View Post
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.
Ingemar is offline   Reply With Quote
Old 06-08-2011, 07:35 AM   #17 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 47
baei1024 is on a distinguished road
Default

Quote:
Originally Posted by RickMaddy View Post
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.
__________________
baei1024 is offline   Reply With Quote
Reply

Bookmarks

Tags
programatically, uiview, uiviewcontroller, without nib, without xib

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 343
8 members and 335 guests
Desert Diva, dre, hain, HemiMG, mottdog, oceanlablight, schmallegory
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:08 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0