Κυριακή 17 Αυγούστου 2008

HelloUniverse - iSpazio Developers Program #1

He begins the iSpazio Developers Program! Periodically (at least) we will give you detailed guides on how to build certain applications. Going forward, (probably in September) thanks to our engineer / programmer we can provide a complete and real live seminars, formarvi in full. 
We start immediately with help for the compilation of "HelloUniverse" 


BACKGROUND: This is not a comprehensive guide sull'SDK, but only a tutorial to create a simple application, for those who, like me, wants to look at the world of programming for iPhone / iPod Touch. The full course all'SDK for users and companies, will arrive shortly on iSpazio. 

TUTORIAL: In this tutorial will learn to: 

1. Create a new project 
2. Creating a view 
3. Creating the controller of sight 
4. Manager of events. 
5. Add the view of the window 
6. Connect the controls on the view to the view controller objects. 
REQUIREMENTS: Here's what you need to get started: 

-- The latest version dell'iPhone SDK (must be registered and logged on the Apple site to download it) 
-- And of course a Mac 

1. Create a new project 
Xcode start opening and closing the welcome screen that appears. Create a new project by clicking on "File -> New Project" and selecting "Window-Based Applications" from "iPhone OS." 
Putting the name of the project "HelloUniverse" and save it. The window you see before you is the IDE Xcode. 


Note: If you called your project in a different way, your files will have a different name than what I say in the tutorial. 

Some of the files needed to run the application has already been created. One of the most important is called "main.m" and a method called "main" which receives two arguments in input and returns a whole. Almost every application for iPhone that is created, will have a method "main" like this one. 


Another important detail of the project is delegated to the application, which in our project is called "HelloUniverseAppDelegate", where the class is declared in the file header (indicated by an extension. "H") and the class is implemented in the file deployment (indicated by an extension. "m"). 
You have just created your first application for iPhone; click on "Build and Go" to find out for themselves. 


What you are seeing is the application running on a simulator dell'iPhone. This does not do anything useful, except to display a white screen. Hours add some features to it. 

Writing applications for iPhone is completely new experience to another device. There are few things that you need to remember, when creating applications for the iPhone: 
1. An application for iPhone can have only a window. We create views that can then be added to the window. 
2. iPhone OS does not support the memory management by using "garbage collection" which is present in Mac OS X v10.5 or later. 


2. Creating a view 
To create a new vision, we need to create a nib file. A nib file is the user interface that designs using Interface Builder, "which comes with the SDK. As the name suggests, "Interface Builder" is a visual tool that is used to create windows, views and establish relations between views and other objects in the application. A nib file has the extension. "Xib." A file is already created for us and is the main window. The same window that we saw in the simulator dell'iPhone. 

Double-click the file "MainWindow.xib" to start the Interface Builder. 


MainWindow.xib has four items: 
1. File's Owner - This is the instance of UIApplicaion. 
2. First Responder - This is used to manage events multi-touch. 
3. Hello Universe App Delegates - Responsible for viewing the window and setting the initial view. It is also responsible for restoring the application to its previous state, control of memory, respond to changes in guiding the device. It is set to an instance of class HelloUniverseAppDelegate. 
4. Window - This is the window you see when you load the application. 

To create a new view using Interface Builder, click on "File -> New". In the left panel select "Cocoa Touch", and then select "View". You'll see the new view just created. 


First, we must save the new view and add it to our project Xcode. To do this, select the view and click on "File -> Save", select the location to save it (in your project), chiamatela "HelloView" and click "Save." As soon as you save the file, you will be asked to add the view to a project Xcode, select the project and click on "Add". 


Go now Xcode and drag the view under "Resources". This is the place where your nib file must be made. 



Now we created our sight. We try to add some controls sight. You can do it with a double-click the newly created view and adding items from the library. If you do not see the library, you can open it by clicking on "Tools -> Library." 

Creaiamo hour graphical interface itself. Inseriamo in our view two "Text field" (fields that allow text entry), a "Label" (label show in which a string) and a "Button." Everything should look more or less like this: 



We set some properties on our checks. 
Select the first text field and select "Attributes Inspector" ( "Tools -> Attributes Inspector"). Insert "First Name" for Placeholder, in the "Text Input Traits" select "Words" to "Capitalize", "Name Phone Hall" to "Type" and "Done" in "Return key." Tap, also, "Clear Context before Drawing" in "View" (This will erase the contents of the field before entering the text). 
Applying the same settings for the second text field, but enter "Last Name" in Placeholder. 
Now, double click on the button and enter "Click Me !!!". This can also be done by selecting the button and modifying the attribute "Title" in ' "Attribute Inspector." 
Finally select the label and make sure that the option "Clear Context before Drawing" is selected. 

We have finished designing our first sight. Saving all Please select "File -> Save." 

3. Creating the controller of sight 

Now we need to create a controller of vision. This controller plays a central role in creating an application for iPhone. They are responsible for the management vision, navigation and memory. Each view is connected to a controller of vision. 
So we start to create our vision controllers. The UIKit provides a class UIViewController that hides most of behaviour by default. To create a new controller sight select Xcode, click on the class, select "File -> New File" and then choose "UIViewControllersubclass", then click on "Next" and enter the name "HelloViewController". 


Now we need to create variables for the checks that we defined in our view. So we can connect these variables controls on sight. 
To do this, open the file headers (which should be called "HelloViewController.h") and add the following code: 


Now, the compilation of your application should succeed, but with some "warnings". 


You can see that the controls are defined by the aprola reserved IBOutlet, which specifies the compiler of these variables make available nell'Interface Builder. We have also defined property for our variables. 

4. Management of events 

We have also declared a method to manage the event click of a button. This method is declared with a special word: IBAction, which tells the compiler to make this method available nell'Interface Builder. The method takes a parameter of type id (generic) and returns void (marked by IBAction). 

Now we need to implement the method and also to synthesize the properties. Insostanza must tell the compiler to create the method and get set. 

Open the file deployment ( "HelloViewController.m") and add the following lines of code after the line "@ implementation HelloViewController": 


Now write the following code to display a message for you: 




The variable sTemp is an object created with the message alloc and initialized with the message "initWithFormat." As you can see "% @" within the string is for the position of variable nth. The logic control test every combination to find the kind of message to be displayed based on user input. Since the string is an instance of NSString and created using alloc, we deallocarla manually. This is done by passing the message "release" to the recipient. 

We also have three properties to be taken into the text field and on the labels of view. Since were with the attribute "retain" we must also release their memory. This is done in the method "dealloc", as shown below. The method "dealloc" is used to free the memory of the objects Objective-C that we create. 





5. Add the view of the window 

Now, we must add seen as a sub-window view of which is shown in the simulator. To do this, open the file "HelloUniverseAppDelegate.h" and created a variable of type "HelloViewController" and also create a property. 
Since we are using the type HelloViewController in the file header, we must warn our files controller sight. We do this by adding a statement later, which indicates the compiler that the class is defined somewhere else and not worry about research now. This statement is added before the start of the string using the "@ class classname." 

In fil implementation, we need to synthesize the property and to issue a memory through the dealloc method. 
Here's what you'll have to add the file "HelloUniverseAppDelegate.h." 


E 'in the method "applicationDidFinishLaunching" we must add our seen as a sub-view window. This is the method that is called when the application has finished its loading and where they should erase the first view that the user sees or restore the application. 
This time the file will be amended "HelloUniverseAppDelegate.m" with the following code: 


Ricapitoliamo then the steps to follow to add a sub-view to a window: 
1. Creating an object of type "HelloViewController" using the command alloc and instanziarlo using "initWithnibName" which loads the nib file "HelloView" from the main package. A package is not relevant to this article, but most of the nib file are loaded from the main package. 
2. Set the view in the property. 
3. Leave the memory. 
4. Then we can take the view from the controller of sight and add it as sub-view window. Here the message of view rose to the controller of view and the view back is added as a sub-view window. 

5. Then we give everything visible. 

6. Connect controls visual objects of sight 

Now we have to connect the controls of sight to the listings that we created. To do this, then double-click on the file "HelloView.xib" which will open the Interface Builder. 

First debbiamo set the controller of sight as "File's Owner Proxy Object." To do this on the screen "HelloView" select "File's Owner", then open, ' "Inspector Identity" in the menu "Tools"; hour, the drop-down menu selected Class "HelloViewController." If we take a second to consider this screen, under "Class Outlets" we see that all our variables defined in the header files are present here. Now we need to connect the controller of sight with the view. To do this click, while the previous panel, on "Connetors" where you will see the variables that we defined earlier. You should have a screen like this: 


At next to "view" you should see an empty circle, click on the circle and drag it on sight, and then release it. You will notice that the connection was made between the variable of view and the view setssa.Fate then the same thing for the variables lblMessage, txtFirstName, txtLastName. 
When this is done, you should have a screen like this: 


Now we need a connection to invoke our method of viewing the message when clicked the button. To do this, select the view and its "Connecion Inspector." Click on the button of sight, among other options in "Connections" should see "Touch Up Inside", which is the event called when the button is clicked. Click the circle next to empty and drag on "File's Owner"; rilascerete when he will appear a "displayMessage", click to create the connection. The connection should look something like the following: 

This is all that we have to do. Let's go back in Xcode and click on "Build and Go." Be sure to have four rows with the code ( "HelloUniverseAppDelegate.h", "HelloUniverseAppDelegate.m", "HelloViewController.h", "HelloViewController.m") inside the folder "Classes". Proceed then the compilation. 
The application must be completed without any mistake and without any warning. The application will be launched, and we can put our name and display the message. 


But we have a problem, it seems that the text editor not understand that it is done editing and does not close himself. To resolve this problem, we need to implement a protocol controller in our sight. 

The way to implement a protocol is surrounding between <> the name after the name of the super class. The protocol that we have implemenatare is "UITextFieldDelegate." They use this protocol can set the "File's Owner" as a delegate of the two text fields. The declaration of the file should appear as something similar (to be done in the file "HelloViewControlelr.h"): 




Now we need to implement the method that will be able to manage the closure of the text editor when you click the button. 
To do so add the following method for the file deployment ( "HelloViewController.m") 


What this code does is to check what field this method is called and even if it is from both camps are sending the message "resignFirstResponder" and return YES, which is similar to the return of a type Boolean. 

Now we should tell the text field to use the "File's Owner" as a delegate. To do this, select the view and click on the "First name" and seleziamo menu "Tools" "Connetions Inspector." We will see a field called "proxies", click on the empty circle on the right and drag it to the "File's Owner", so that the connection is created. Run then the same operation with the "Last name". You'll have a screen like this: 


Save the application and click on "Build and Go." Now the application will work to perfection! 

The guide was translated by Andrea Busi for iSpazio.net. Finding the original version to this address: iPhone SDK Articles: First iPhone Application. The relatively Meriti then to the English version, are of legitimate author.

iphone pen


This is good news coming from TUAW. A reader has notified them a new pop-up in respect of Notes present in Mail for Mac. A next function dell'iPhone OS will be precisely the synchronization of notes, and fill another failure. 


The screenshot above shows a notification system regarding the conversion of the format of the note. Please note that once converted into rich text format, you can not change the note dall'iPhone. The "Sync Notes", was part of "to do list" with regard to firmware 2.0. Evidently not been able to finirlo time, and therefore will be included in the next release of firmware.

iphone pen

On ThinkGeek.com is a pen through which you can check your iPhone / iPod Touch. Although the two gioiellini home Apple have been designed and constructed to be used simply with your fingers, there are many people who for various reasons, one of the most banal could be such as nails long, fail to get used to control the device through the finger. iPhone Japanese Touch Pen Stylus is a flexible pen that simulates perfectly the touch of your fingers, making control dell'iPhone / Touch iPod a breeze. The pen, available in colors silver and black, about 10 cm long and constructed entirely of metal (except the part that touch screen devices), also includes a very useful tool that will allow you to open the door of sim without too many problems and costs $ 14.99. If you are interested, here you will find more information and how to purchase.