Mar 29

Here I will discuss each and every thing that is required to create a business application for iPhone. If you have tried any business application or if you have read the User Interface Guide on Apple’s developer site, you will agree that almost all the business applications invlolve atleast one UITableView. Also the UI is very neatly designed and does not involve any flashy UI elements.

So In next few posts I am going to create each of those elements required to make a successful business application and at the end I will be demostrating a sample application that downloads data from a server and displays the information in a neat way.

Following is the list of topics under this catogory:

In all my tutorials, I will be using generic template that is “View Based”. The reason being that you have full control over every thing. I will not be using Interface Builder which does not allow me to customize tables and other elements exactly the way I want.

To start with I will be writing a simple Hello World Application here.

I will create a simple view with a label and a button. The Label says Hello World and a Button which says Hit Me, when clicked prints a log statement on console.

I am going to use this project in all my next posts where I will be building complex views, custom table views with custom cells, animations, webViews etc.
The application I am going to build will finally look like this

screen-capture-2

So open the xCode and create a new project. Chose the template “View Based” and name the project as “MyTestProject”
This will open a new project with may folders appearing on the left side of xCode. Expand the classes folder and select MyTestProjectViewController.h. Add the following code to it:
We have defined a label and a button in the header file. In the implementation file we will create them and finally add it to the baseView. So open MyTestProjectViewController.m and add following code within the implementaion declaration i.e. after
@implementation MyTestProjectViewController.
Most of the lines in code are self explanatory. I will explain some of them:
aLabel = [[UILabel alloc]initWithFrame:CGRectMake(90,100,200,40)];
here you are actually creating a Label and initializing it with a frame. A frame represents the lay out of a UI element. The first 2 arguments of CGRectMake are (x,y) coordinates from where the label will appear. The last 2 arguments are (width,height) size and they define how much space will they be taking on the view.
[aLabel setBackgroundColor:[UIColor whiteColor]];
Yes a label also has a background color. When you define a frame, you are creating a rectangle with its left corner at the (x,y) and right corner at (x+width, y+height). The label (or any UI element) lies inside a frame. The background color of a label is the color with which this rectangle will be filled.
[aButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
Now in this line we are telling what a button should do on being pressed. addTarget:self tells that the event will be handled by the same class in which it is being declared.
action:@selector(buttonPressed) tells that when the button is touchedUPInside this method should be called automatically.

-(void)buttonPressed

{

NSLog(@”button was pressed”);

}
Finally we have implemented the method which will be called automatically when the button is pressed. You can write anything inside this method.
I have not used Interface Builder at all. If you want to do some serious development you need to learn how every thing is done programatically,although IB is one very important tool. In the next post I will be creating another View and add some more Elements to it and we will be adding code for navigating from one view to the other.
Let me know if there is any specific issue.


26 Responses to “iPhone Tutorial: Everything you need for a Business Application”

  1. [...] Simple Hello World Application [...]

  2. [...] iPhone Tutorial: Everything you need for a Business Application iPhone Tutorial: Customizing Header View Mar 29 [...]

  3. [...] iPhone Tutorial: Everything you need for a Business Application iPhone Tutorial: Customizing Header View Mar 29 [...]

  4. [...] iPhone Tutorial: Everything you need for a Business Application iPhone Tutorial: Customizing Header View Mar 29 [...]

  5. lowell says:

    “If you want to do some serious development you need to learn how every thing is done programatically.”

    True, but usage of IB is actually encouraged for Cocoa developers. IB doesn’t generate code, so you’re actually being more efficient by using it rather than coding the UI in Objective-C. We freeze dry our UIs here.

    You are welcome to verify my statement with Apple directly through DTS, or with any Cocoa developer who’s been working with Cocoa or Carbon for longer than the iPhone has been out.

    I understand that .NET and Java IDEs generate code for their UIs; this is a whole different ball game, my friend. Welcome aboard! There’s a lot of new people – please don’t mislead them. :)

  6. webscale says:

    No offences Lowell,what u said is true but what I want to emphasize here is that one MUST learn how to do everything through code.

  7. lowell says:

    Ah, none taken. :)

    See, when working across platforms it’s common to have concepts and conventions form one influence the other; an example is using camelCase for variable names in Ruby. You’re not supposed to because underscores are the convention, but for the most part, the interpreter doesn’t care. At worst, you’ll annoy the rest of the Ruby world, but at least the code builds – and that’s the goal.

    This isn’t the same thing. At all.

    Learning how to construct UI through code is *not* an essential part of learning Cocoa; and it goes back to IB not generating code. By writing code to construct a UI, the developer will (a) create more work for him/herself and (b) create more work for the compiler (because it now has to build a UI that the developer didn’t build in the first place).

    Hand coding to skip Interface Builder should be avoided except in cases where you have extremely complex UIs and writing it is more efficient. I can see where, another example, teaching Javascript before jQuery is a *must*, because they are related – one is built upon the other. IB nib/xib files are *not* built upon code. It isn’t a matter of personal preference or opinion nor should it be used as a measure of competency, because it is *not*.

    I’ve been needing a good topic to blog about; you’ve definitely given me inspiration lol! Thanks!

  8. ABC says:

    Lowell,nice that you got one :)
    In my next posts it is definitely going to be clear why using IB in some cases where you want everything exactly some way, can create problems for you.

    And you must have noticed, I did mention to write from code when you need to customize UI elements to your need.

  9. [...] Simple Hello World Application [...]

  10. [...] Simple Hello World Application [...]

  11. remover says:

    I have tried this in its most simplified version but my program keeps crashing:

    all i did was create a view based app and then added the following lines to the loadview method:

    - (void)loadView {

    thelabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 40)];
    thelabel.text = @”its there”;
    [self.view addSubview:thelabel];
    }

    I just wanted to see if i could create anything programmatically. Could you please tell me why this is not working.

    Thx.

  12. remover says:

    of course… i added the instance variable to the header file also….

  13. ABC says:

    Try adding following code on the very first line
    [super loadView];

  14. remover says:

    adding [super loadview] prevents it from crashing but i see no label…

  15. ABC says:

    Create a contentView and use it as your view.
    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    UIView *contentView = [[UIView alloc] initWithFrame:screenRect];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.autoresizesSubviews = YES;
    contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.view = contentView;

  16. Bik says:

    Wonderfull…

  17. Holtzclaw says:

    Nice way that you present your ideas. You have a well set out blog and I will check back again. Thank you. brite smile

  18. ABC says:

    Thanks Holtzclaw :)

  19. Depaolo says:

    Good information, thanks for the post!

  20. Englin says:

    Nice blog and thanks for the post!

  21. Brian says:

    This is exactly what I have been looking for in a tutorial! Do you know when you will be adding the web service section?

  22. ABC says:

    Yes, I kind of got consumed, bt going 2 start with remaining ones and a fresh new gameDev tutorial :)

Leave a Reply

preload preload preload