In this first iPhone app tutorial you will be introduced to some new tools like xcode, and Interface Builder. You will also learn how to create new view, add controls, and respond to the events. Click on Read more to continue.Introduction
In this tutorial I will give you a brief introduction on how to get started with your first iPhone application. To begin you will need the latest version of the iPhone SDK which you can download it from here. With the SDK you get some tools like Xcode, Interface Builder, iPhone simulator, and many more. The first application is usually called “Hello World” but I have named my first app “Hello Universe” because a revolutionary device calls for a change of name.

Purpose of the “Hello Universe” app
Using the app a user will be able to enter his/her full name and click a button to see a message appear. The message will say “John Doe says Hello Universe!!!”. This app will not only introduce you to some of the tools but will also show you how to use controls, respond to events and create new views. Excited, I am :-)

This is how the app will look like

Creating a new project
Launch Xcode and click on File -> New Project -> Select Application (under iPhone OS) -> select Window-Based Application project template and click on choose.

In the next screen you will be asked to save your project and give it a name. Xcode creates some files for us based on the name of the project, so you want to be careful with the name you provide. I have named my project “HelloUniverse”.

This is how the list of files look like in Xcode.

All the class files are stored under the “Classes” folder, some special files are listed under “Other Sources”, all the view files and resources show up under “Resources”, and any library or frameworks we add to our project are listed under the “Frameworks” folder. It is important that we save all the images, files, databases, and views in the “Resources” folder because all the iPhone apps run in its own sand box; which means they can only access files placed under the resources folder.

How the app is launched
Every C/C++/Java/C# programmer knows about the main method found in main.m file which is present under the “Other Sources” folder. You normally will never have to change this method and all we have to know is that this method is responsible in launching the app. The method applicationDidFinishLaunching method is called when the app is launched on the device or the simulator. The method is defined in “HelloUniverseAppDelegate.m” file which is found under the “Classes” folder.

Interface Builder
Using Interface Builder we can design our application by adding controls or creating additional views. The files that the Interface Builder creates gets saved with a .xib extension and are called nib files. Every project gets one nib file by called which is called “MainWindow.xib” which can be found under “Resources”. An iPhone application has only one window (MainWindow.xib) unlike a desktop application which is created with multiple windows; however, we can create multiple views which are added to the window. Double click on “MainWindow.xib” to launch the Interface Builder, which will open four windows and one of the window will look like this


The above picture shows the contents of the “MainWindow.xib” nib file. Every nib file has atleast two files; File’s Owner and First Responder which cannot be deleted. Every other objects apart from the first two, represents an instance of an object which gets created when the nib file loads. File’s Owner simply shows that it owns the object in the nib file. First Responder tells us which object are we currently interacting with; like the textbox, buttons… The third object which is special to the MainWindow.xib file is called “Hello Universe App Delegate” and this file represents “HelloUniverseAppDelegate” class. Last but not the least the view represents the object which we design in our apps.

Creating a new view
If we had created this project using “View-Based Application” project template then there would have been no need of creating another view from scratch, but where is the fun in that. In Interface Builder create a new view by clicking File -> New -> Select Cocoa Touch -> View and click on choose. Let’s save the view in the project folder by naming it “HelloUniverse” and once we do that IB (Interface Builder) will prompt us to add the view to the current project; click on “Add” and it will show up in Xcode. In Xcode and move your view to the Resources folder.

Creating a view controller
We have a view now let’s create a view controller to manage the view. In Xcode create a new view controller by selecting classes and clicking File -> New File -> Select Cocoa Touch Classes under iPhone OS -> select UIViewController -> click on choose and name your file “HelloUniverseController” without changing the extension. The newly created class inherits from UIViewController which knows how to interact with a view. Now that we have our view and the controller class, there must be some way to connect these two files and we can do it by setting the class property of the File’s Owner. Double click “HelloUniverse.xib” file in Xcode to launch Interface Builder and select File’s Owner -> select Tools -> Identity Inspector and under “Class Identity” category, change the class to “HelloUniverseController”. This is how the Class Identity should look like


Once that is done we need a way to control the view in the nib file from code and this is done by connecting the view instance variable to the view object in the nib. The connection is made using “outlets”. Select Tools -> Connections Inspector create a connection from the view variable to the view in the nib file. Move your mouse over the empty circle to see it change to a plus symbol indicating that a connection can be created. Click on circle and drag your mouse to the view in the nib file and release. As you do this you will see a blue line being created from the circle to the mouse. Once a connection is created, the connections inspector for File’s Owner will look like this


Adding controls to the view
From the screen shot above we require two text boxes, one label, and a button (Round Rect Button). From the library drag and drop the controls to the view and align it as seen in the figure 1.0. After you have added the controls let’s change some of its properties, starting with the text boxes. Select the first text box and open Attributes Inspector by selecting Tools -> Attributes Inspector. Under Placeholder enter “First name”, under “Text Input Traits” change Capitalize property to “Words” and change the Return key property to “Done”. Apply the same settings for the other text box but change the Placeholder to say “Last name”. Select the label and delete its text property, since we do not want the label to say anything until the button is clicked. Double-click the button to edit the title of the button and type in “Click Me”. Save and quit Interface Builder as we have successfully designed our view.

Connecting instance variables to the objects in the view
We still need some way to interact with the controls on the view, in code and this is where outlets help us out. IBOutlet is a special keyword if used with an instance variable, will make the variable appear in Interface Builder. Since IBOutlet makes the variable appear in Interface Builder, using IBAction as a return type for a method will have the opposite effect. Using IBAction we can handle an event triggered by any control placed on the view. Let’s see how this works; open HelloUniverseController.h file in Xcode and type in the following code

//HelloUniverseController.h
@interface HelloUniverseController : UIViewController {

IBOutlet UITextField *txtFirstName;
IBOutlet UITextField *txtLastName;
IBOutlet UILabel *lblMessage;
}

- (IBAction) btnClickMe_Clicked:(id)sender;

@end

//HelloUniverseController.m
- (void)dealloc {
[txtFirstName release];
[txtLastName release];
[lblMessage release];
[super dealloc];
}

All the variables above are marked with IBOutlet and Interface Builder will make these available to itself so proper connections can be made. We also have a method whose return type is IBAction (void); Interface Builder will also make this method available to itself so we can choose which event will call this method. The method also takes a parameter called “sender” which is the object which triggered the event. The variables are released in the dealloc method as shown above. Let’s connect these instance variables to the controls on the view (HelloUniverse) as described earlier. Open Interface Builder by double-clicking HelloUniverse.xib file and select File’s Owner, open Connections Inspector to see all the variables present under Outlets and to create connections.


Let’s hook up the button click event to the “btnClickMe_Clicked” method. Select the button and under the Events list it seems that we do not have a button click event; however, we do have a “Touch Up Inside” event which is raised when the button is touched and released symbolizing a click. With the button selected click the circle next to “Touch Up Inside” and drag your mouse over to File’s Owner and release to have the method name show; simply click on the method name to create a connection.


Handling events
Let’s write some code in btnClickMe_Clicked event to read the first and last name and display a message in the label. This is how the code looks like

//HelloUniverseController.m
- (IBAction) btnClickMe_Clicked:(id)sender {

NSString *FirstName = txtFirstName.text;
NSString *LastName = txtLastName.text;
NSString *Message = nil;

if([FirstName length] == 0 && [LastName length] == 0)
Message = [[NSString alloc] initWithFormat:@"Anonymous says Hello Universe!!!"];
else if ([FirstName length] > 0 && [LastName length] ==0)
Message = [[NSString alloc] initWithFormat:@"%@ says Hello Universe", FirstName];
else if ([FirstName length] == 0 && [LastName length] == 0)
Message = [[NSString alloc] initWithFormat:@"%@ says Hello Universe", LastName];
else
Message = [[NSString alloc] initWithFormat:@"%@ %@ says Hello Universe", FirstName, LastName];

lblMessage.text = Message;

//Release the object
[Message release];
}

The method does few simple things, it finds out the first and last names and figures out what message to display in the label. We also create a temporary variable called “Message” to hold the message which will be displayed in the label. The variable is allocated and initialized by alloc and initWithFormat messages respectively. The variable is released in the end because when working with the iPhone we are responsible of cleaning up the memory. The easiest way to remember when to release objects is; if you create it then you own the object and hence you are responsible of releasing it.

If you click on Build and Go, the view will not be visible because we have not yet added it to the window. Let’s see how we can do that

Adding view to the window
Now we cannot drag the view and add it to the window, so we need another way to add the view as a sub view to the window. We already know that “HelloUniverseController” is the view controller of the view “HelloUniverse” and “HelloUniverseAppDelegate” is the application delegate where the window is made visible in applicationDidFinishLaunching method. It is in that method we will add the view as a sub view to the window. Before we do that, the application delegate (HelloUniverseAppDelegate) needs to know about our view controller. Add the following lines to HelloUniverseAppDelegate.h file to change the file like this

//HelloUniverseAppDelegate.h
@class HelloUniverseController;

@interface HelloUniverseAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
HelloUniverseController *hvController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) HelloUniverseController *hvController;

@end

From the above code we first add a forward class declaration of “HelloUniverseController” because we do not want any circular dependency when we import the header file of “HelloUniverseController” in HelloUniverseAppDelegate.m. A variable and a property of type HelloUniverseController is also declared. The property as you can see is declared with a couple of attributes called “retain” and “nonatomic”. The retain attribute will increase the reference count of the instance variable by one and nonatomic is used because our program is not multi-threaded.

A lot of first time users miss to synthesize the property we declared, so let’s do that now at the top of the HelloUniverseAppDelegate.m file and the code looks like this


//HelloUniverseAppDelegate.m
@implementation HelloUniverseAppDelegate

@synthesize window, hvController;
...

The view is added as a subview to the window in applicationDidFinishLaunching method and this is how the code looks like

//HelloUniverseAppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {

HelloUniverseController *hvc = [[HelloUniverseController alloc]
initWithNibName:@"HelloUniverse" bundle:[NSBundle mainBundle]];

self.hvController = hvc;

[hvc release];

[window addSubview:[self.hvController view]];

// Override point for customization after application launch
[window makeKeyAndVisible];
}

The second line shows that we imported the header file of “HelloUniverseController” and we also synthesized the property hvController. In the “olden” days in order to create a property we had to create getter and setter methods which would be something like getHVController and setHVController. The synthesize keyword automatically generates these methods for us. In applicationDidFinishLaunching method we allocate and initialize a temporary variable, assign it to our property, release it, and the view associated with the view controller is added as a sub view to the window. The key thing to remember here is that the view message is passed to the “hvController” which returns the view and is added as sub view to the window. A valid view is returned because we created a connection from the view instance variable to the view in the nib file. The property is finally released in the dealloc method as shown below

//HelloUniverseAppDelegate.m
- (void)dealloc {
[hvController release];
[window release];
[super dealloc];
}

We have always allocated and initialized variables in one single line as seen below, this is usually the accepted way but the same code can be written in a different way as seen below

HelloUniverseController *hvc = [HelloUniverseController alloc];
hvc = [hvc initWithNibName:@"HelloUniverse" bundle:[NSBundle mainBundle]];

Build and go to test your application.

Hiding the keyboard
Wait a minute clicking the “Done” button doesn’t do anything. Ideally we would like to hide the keyboard when the “Done” button is clicked no matter which text box we are currently editing. To hide the keyboard we need to do two things; set the delegate of the keyboard to File’s Owner and implement a method called textFieldShouldReturn in HelloUniverseController.m file. To set the delegate, open Interface Builder by double clicking HelloUniverse.xib, select the first text box and click on Tools -> Connections Inspector. Click and drag your mouse by selecting the empty circle next to delegate under “Outlets” and release your mouse over to File’s Owner. Assign the delegate for the next text box in the same manner.

The method textFieldShouldReturn gets called when the “Done” button is clicked and this is how the code looks like

//HelloUniverseController.m
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

[theTextField resignFirstResponder];
return YES;
}

The method gets a parameter called sender, which is the object that triggered the event. We will use the same object to which we send the resignResponder message, which will hide the keyboard on the textbox. The boolean “Yes” is returned to tell the sender that the first responder is resigned.

Conclusion
I hope you had fun with this tutorial and feel a little confident in writing your next great iPhone app. Please leave me your comments and let me know what you thought.

Happy Programming,
iPhone SDK Articles

PS: I have re-written this tutorial hoping it would be easier to follow. If you have any questions please do not hesitate to send me an email here and I will be happy to help you out.


Attachments

Suggested Readings

 

149 Responses to First iPhone Application

  1. Anthony says:

    This is excellent work.. Thank you very much.

    Have you any plans to produce any more additions to this tutorial?

  2. almanya chat says:

    I do not have the iPhone OS option

  3. Thanks for the reupload link and it work great :)

  4. Saif says:

    Excellent job…

  5. edweirdo says:

    >There is a statement:
    “Let’s connect these instance variables to the controls on the view (HelloUniverse) as described earlier”

    You might want to call that out a bit more. It’s one line buried in a paragraph. Without connecting the instance variables to the controls on the view, no data is passed :)

    ———————–

    @Davide:

    You need to add the synthesize line that other folks have mentioned. The example is still missing it:

    // HelloUniverseAppDelegate.m

    @synthesize hvController;

    That line adds setters and getters to hvController.

  6. Davide says:

    >Hi!! :)
    First of all… this is GREAT!
    Thank you for this tutorial…
    even if it doesn’t work for me =)

    This is what I get in the console:

    2009-04-29 23:57:29.172 HelloUniverse[29346:20b] *** -[HelloUniverseAppDelegate setHvController:]: unrecognized selector sent to instance 0x5234c0
    2009-04-29 23:57:29.173 HelloUniverse[29346:20b] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** -[HelloUniverseAppDelegate setHvController:]: unrecognized selector sent to instance 0x5234c0′
    2009-04-29 23:57:29.175 HelloUniverse[29346:20b] Stack: (
    2489135371,
    2451058235,
    2489164554,
    2489157900,
    2489158098,
    9560,
    816111650,
    816149355,
    2502230574,
    2488638245,
    2488638680,
    827745792,
    827745989,
    816114848,
    816160924,
    9380,
    9234
    )

    Sorry for the long log… Hope you can help me!

  7. inertia says:

    >Man, if not by this, how am I supposed to know all these things? Thank you very much for creating the step-by-step that Apple itself didn’t do.

  8. Anonymous says:

    >One of the neat & best iPhone dev tutorials!! Good Work.. Please keep writing more tutorials!!

  9. Anonymous says:

    >I downloaded and installed Xcode but I do not have the iPhone OS option. What now?

  10. says:

    >Hi I am curious if there is anything I need to set/config in order to get “done” on the keyboard instead of “return”. Mine shows “return”, but when I run your code it shows “done” instead. Hence, I am very curious about it.

    Thanks.

  11. agnes says:

    >Hi, I have a question, why is that yours shows “done” on the keyboard, but mine shows “return” on the keyboard (on the simulator). Is there anything I need to configure?

  12. Anonymous says:

    >Hello. This is really great stuff. Very informative and easy to follow. With that said, I actually have a problem that is not shown here.

    "variable to the view object in the nib. The connection is made using "outlets". Select Tools -> Connections Inspector create a connection from the view variable to the view in the nib file. Move your mouse over the empty circle to see it change to a plus symbol indicating that a connection can be created. Click on circle and drag your mouse to the view in the nib file and release. As you do this you will see a blue line being created from the circle to the mouse. Once a connection is created, the connections inspector for File's Owner will look like this."

    When I do this, and i drag the circle to the file's owner, the box doesn't appear to link them, thus no methods are shown. Any Idea what I could have done wrong?

  13. Anonymous says:

    >The tutorial forgets to mention that the File owner need to bind outlets to all the text fields. I can get the whole thing to work using UITextField objects for both the input and output. However, the tutorial says to use a UILabel object for the output, which does not appear to work. A UILabel does not appear to be a valid outlet for bindings… does something need to be set to make the UILabel dynamic, or am I missing something obvious? Thanks, otherwise, really helpful tutorial.

  14. Anonymous says:

    >[quote]Article doesnt mention:

    // HelloUniverseAppDelegate.m

    #import “HelloUniverseAppDelegate.h”
    #import “HelloUniverseController.h”;

    @implementation HelloUniverseAppDelegate

    @synthesize window, hvController;[/quote]

    This is crucial – thanks for posting!
    It’s obviously missing in the original Article causing the simulator to crash…

    Apart from that – I still don’t get it… Guess I have to read a book about Objective C… :)

  15. DaveP says:

    >Thank you so much. This has been absolutely invaluable in helping start on the road to developing with the SDK.

  16. Tom says:

    >hey, loved the tutorial! Just one little problem though..

    - (IBAction) btnClickMe_Clicked:(id)sender {

    NSString *FirstName = txtFirstName.text;
    NSString *LastName = txtLastName.text;
    NSString *Message = nil;

    if([FirstName length] == 0 && [LastName length] == 0)
    Message = [[NSString alloc] initWithFormat:@"Anonymous says Hello Universe!!!"];
    else if ([FirstName length] > 0 && [LastName length] == 0)
    Message = [[NSString alloc] initWithFormat:@"%@ says Hello Universe", FirstName];
    else if ([LastName length] > 0 && [FirstName length] == 0)
    Message = [[NSString alloc] initWithFormat:@"%@ says Hello Universe", LastName];
    else
    Message = [[NSString alloc] initWithFormat:@"%@ %@ says Hello Universe!", FirstName, LastName];

    lblMessage.text = Message;

    [Message release];
    }

    When I execute that code, XCode says execution 'stopped at breakpoint 1' (which apparently is lblMessage.text = Message;). Why does it do this?

    Also, the

    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder];
    return YES;
    }

    doesn't seem to work either. I'll post more code if it helps :)

    Thanks

  17. Anonymous says:

    >Good article, but as others have said there are some missing information:

    In HelloUniverseAppDelegate.m you need this line next to the other import:

    #import “HelloUniverseController.h”

    Also you need to synthesize the hvController:

    @synthesize window, hvController;

    Also, the article skips the step where you connect the reference outlets to the IBOutlets. You need to open the connection inspector and drag the connection between the controls for txtFirstName/txtLastName/lblMessage.

    Aside from these issues, great tutorial.

  18. jason oda says:

    >I keep getting stuck at the end of the “Create a view controller” step because every time I do this, only navigationItem and tabBarItem come up as Outlets. Furthermore, when I continue on despite this, I can not link my Touch Up Inside to File’s Owner. It doesn’t accept it as an option and won’t highlight it when I drag over it. Please help.

  19. iPhone SDK Articles says:

    >@Anonymous All the methods should be implemented in .m file. It looks like that dealloc method is declared in .h file but it should be in .m file.

    Hope this helps.

    Happy Programming,
    iPhone SDK Articles

  20. Anonymous says:

    >Each time I try to Build and Go I get the following error within HelloUniverseAppDeligate.m

    - (void)dealloc {
    Fatal error: method definition not in @implementation context
    [hvController release];
    [window release];
    [super dealloc];
    }

    How do I fix this?

  21. iPhone SDK Articles says:

    >@dmx You can send me an email with the code and I will be happy to help.

    Happy Programming,
    iPhone SDK Articles

  22. dmx says:

    >Hi,
    This tutorial really is truly excllent in explaining the concept of apps in terms of views for newbies like me. The unfortunate thing is I tried it 3 times and I get the same problem. All the simulator shows is a black screen and Xcode shows an exception: __TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__

    Any guidance on the matter? I could post my code.
    Thanks,
    Manny

  23. Anonymous says:

    >Article doesnt mention:

    // HelloUniverseAppDelegate.m

    #import “HelloUniverseAppDelegate.h”
    #import “HelloUniverseController.h”;

    @implementation HelloUniverseAppDelegate

    @synthesize window, hvController;

  24. jakudo says:

    >Thx for this tutorial. I’m total beginner and this helps a lot!

  25. sohbet says:

    >Sohbet,chat

  26. iPhone SDK Articles says:

    >I think I have an old copy, please send me your email address and I will be happy to email it to you. Feel free to send me an email with any questions you have, I may take some time to reply but I will reply.

    Happy Programming,
    iPhone SDK Articles

  27. iPhone SDK Articles says:

    >@gecko Glad you found this tutorial helpful.

    It is a good idea to create a temporary variable and then do the assignment rather then initialize the variable directly. The reason, memory is very expensive on the iPhone and there is no automatic garbage collection.

    Try this code out

    self.hvController = [[HelloUniverseController alloc]
    initWithNibName:@”HelloUniverse” bundle:[NSBundle mainBundle]];

    NSLog(@”Reference Count: %i”, [self.hvController retainCount]);

    Here the object is initialized directly but its reference count is 2 instead of one. So when this object is released it will decrease its count by one. This means this object will still be in the memory after the application exists causing a memory leak.

    I hope this helps.

    Happy Programming,
    iPhone SDK Articles

  28. Anonymous says:

    >Would you mind making the original tutorial available? I was able to get that one to work. I referred a friend to your site, but this version seems to be missing some steps that made the original one more clear. Thanks.

  29. gecko says:

    >Easily the most useful information I’ve come across so far. Thanks for putting this site together. I managed to get this app running with no errors the first time through.

    Only potential for confusion I could see: in the “Connecting instance variables to the objects in the view” section, it doesn’t seem clear that you also have to connect the text field instances to the corresponding variables in File’s Owner (though it is somewhat obvious).

    I am curious, as another reader asked, is there any reason when allocating an object to first assign it to a temporary local variable, only to then immediately assign it to the instance variable? For example, in the applicationDidFinishLaunching method for the hello universe controller object. Seems more elegant to just assign directly to the self variable?

  30. iPhone SDK Articles says:

    >@Alibob150 Sorry you had trouble following this tutorial. If you would send me an email I will be happy to answer your questions and look at your code.

    Happy Programming,
    iPhone SDK Articles

  31. iPhone SDK Articles says:

    >@Kakei Yes you would write the code for btnClickME_Clicked in “HelloUniverseController.m” file.

    Happy Programming,
    iPhone SDK Articles

  32. Kakei Juzo says:

    >Hey, I had a question. Does anyone know where to add the code for btnClickMe_Clicked? Is it in HelloUniverseController.m?

    Thanks in advance.

  33. Alibob150 says:

    >Hiya,

    Firstly congratulations for creating great tutorials!

    However, I first began following this tutorial a few weeks ago, and came back to it today to discover the tutorial had been re-written!

    Unfortunately, whilst before I was following the tutorial perfectly I’m now finding it impossible to complete! I’m finding it unclear where exactly code should be placed, and I keep getting build errors! I’ve had to restart the tutorial twice now and am still not getting it! The code samples you give do not show how this code fits in with the existing automatically generated code! It is my assumption that where you use //HelloUniverseAppDelegate.m, for example, the code should be placed in this file. But should this be instead of the code there, or as well as? I’m getting build errors on automatically generated code such as #import “HelloUniverseAppDelegate.h” in the AppDelegate.m file! It’s all getting a little frustrating and I’m getting close to giving up…

    I hope perhaps you may be able to make things slightly clearer for beginners?

    Thanks very much!

  34. Escorts says:

    >A great tutorial for newbies. A need taking into account how difficult it is for a non programmer to approach building an IPhone App. Thx.

  35. Balinder says:

    >Great tutorial. I have never worked with Objective C before and after a few times trying i was able to follow this and create my first iphone app. I would like to learn about getting long. and lat. of the current iphone location and display on screen or open google map with current location. Anyone got tutorial for location based services. Apple’s one is not clear why we are doing this and how if you see what I mean…

  36. iPhone SDK Articles says:

    >@fabio you need to sign up for the developer program http://developer.apple.com/iphone/

    Happy Programming,
    iPhone SDK Articles

  37. fabio says:

    >How i send the app to a real iphone?

  38. Anonymous says:

    >Thank you very much for posting this tutorial. I just started using it last night and was having some issues getting it to work. Now I see that you have redone the posting and it looks even easier to follow.

  39. iPhone SDK Articles says:

    >@reetu is it possible for you to send me your source code so I can take a closer look?

    Happy Programming,
    iPhone SDK Articles

  40. iPhone SDK Articles says:

    >Hello All,

    I have re-written this tutorial hoping it would be easy to follow. I know writing your first iPhone can be a huge task but trust me it gets easier :-)

    Thanks for all your support.

    Please let me know if you have any questions/comments with the new tutorial and the source code.

    Happy Programming,
    iPhone SDK Articles

  41. reetu says:

    >Hello there. I tried adding manually in your code , some txt field using floowing code and ofcourse I defined variable of UITextField *textFieldSecure in header file and synthesized it in .m file. but all it shows me is the blank white screen, can you please help me about how to add UITextField manually ? or what am I missing in my code.
    [code]

    - (UITextField *)createTextField_Secure
    {
    CGRect frame = CGRectMake(50.0, 50.0, 50.0, 50.0);
    textFieldSecure = [[UITextField alloc] initWithFrame:frame];

    textFieldSecure.borderStyle = UITextBorderStyleBezel;
    textFieldSecure.textColor = [UIColor blackColor];
    textFieldSecure.font = [UIFont systemFontOfSize:9.0];

    textFieldSecure.backgroundColor = [UIColor whiteColor];

    textFieldSecure.keyboardType = UIKeyboardTypeDefault;
    textFieldSecure.returnKeyType = UIReturnKeyDone;

    textFieldSecure.secureTextEntry = YES; // make the text entry secure (bullets)

    textFieldSecure.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right

    return textFieldSecure;
    }
    - (void)loadView {

    textFieldSecure = [self createTextField_Secure];
    }

    [/code]

  42. Nicholas says:

    >@Black Screen Folks

    I was getting the black screen too. Now, I don't know how many different factors can cause this problem, but I found a small discrepancy between the project I created following this tutorial and the downloaded source code. Once I fixed my source, it worked perfectly. ->In the AppDelegate header make sure that you don't use the IBOutlet when using the property directive for the window object. E.G:
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    Should be:
    @property (nonatomic, retain) UIWindow *window;

    I hope that helps,

    -Nick

  43. Tony Fabeen says:

    >nice tutorial..

    there is another error on the tutorial on file “HelloUniverseAppDelegate.m” you should
    put #import “HelloViewController.h” after #import “HelloUniverseAppDelegate.h” declaration..

    it’s really a nice job..

    congrats..

    Tony.

  44. iPhone Application Developer says:

    >Nice article!

    Sam Shaw
    iPhone Application Developer

  45. reetu says:

    >yup need to add * in line given below in the code for viewcontroller.
    @property (nonatomic, retain) untitledviewcontroller *viewcontroller;

  46. Anonymous says:

    >Really nice tutorial. I just spent the last week pouring through the iPhone developer documentation and videos to cover the fundamentals. Having this walk through really helped me understand the work flow involved.

    For those of you having trouble finding the HelloViewController in the drop down, or setting up the connections, make sure it was a “Cocoa Touch View” that you created as opposed to the “Cocoa View”. I made this mistake but was able to fix it by deleting the original HelloView.xib and recreating it.

  47. A. Jesse Jiryu Davis says:

    >I followed your instructions, “Go to Xcode and drag your view under resources. This is where your nib files should be placed.” A few steps later I compiled and ran the code, and saw, “Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[UIViewController _loadViewFromNibNamed:bundle:] was unable to load a nib named” etc. Deleting the reference from the resources group (while retaining the actual file and its reference in the NIB Files group) fixed the problem.

  48. Anthony says:

    >Ah I got it! I was right about the error message and the black screen. The view was not hooked up to the view controller. The reason I was getting the error message is that I didnt save the changes I was making in the Interface Builder. I figured that ‘Build and Go’ would save the *.xib files as well as the source, but it only saves the source. So just make sure you save your changes in both programs!