Showing posts with label iPod. Show all posts
Showing posts with label iPod. Show all posts

Sunday, April 1, 2012


Now that we have created a simple window base application we will move on to create a simple Tab Bar Application. To start with open XCode again and create a Tab Bar Application like below:
Tab Bar App
Then I am going to call mine MyNewTabApp, but feel free to name yours whatever is the most appropriate for you.
Name Tab App
Now for this tutorial I am going to use Interface builder a little bit. Click on the MainWindow.xib file. it should look like this afterwards:
Main Window XIB
If it might look like this:
Main Window XIB Alternate
If this is the case I recommend pulling the small vertical bar on the left of the graph paper to the right until it looks like the first picture. Ok, now that we XCode looking the way we want lets dive into creating the Tab Application. If we build and run the app is it is created for us, we have two tabs and views that are associated with them. The tabs say First View and Second View and you can toggle between them by pressing the tabs at the bottom.
Basic Tab Bar App
Now if our goal is to create a simple application that has four pages we will need to add two more pages to our app. To start this control click on the MyNewTabApp folder in the file view. Then click “New File…” choose the UIViewController subclass option and click next.
UIViewController
Make sure that the “With XIB for user interface has been chosen” and click next.
XIB Checked
I usually put in ThirdViewController as the name especially when creating very simple applications, but feel free to use whatever name you want. Then choose save. Make sure to repeat these steps again for the fourth tab. Now that we have all the view created lets add some stuff to views three and four so we know they are displaying. For view three we will add a Label. Click on the Third View’s xib file and then the view object. Open the Utilities view by clicking on the third View option in the top right of XCode. This will open all the utilities on the right of the IDE. Go down to the objects on the bottom right and drag the label object over to the view. You can make the label say whatever you want. It should look like this:
Setting Up Label
For view four we are going to change the background color. To do this click on the Fourth View’s xib file. Then click on the View object. Next make sure that you have the Utilities View open and click on the Attributes Inspector on the right. You should see a section for view and in that section there is an option for background color. Change that to something that stands out. I chose green:
Setting Up Background Color
Now go back to the Mainwindow.xib file we are ready to link our Tab Bar Controller with the two new View Controller. Go down to the objects in the bottom right hand corner:
View Controller Highlighted
Drag the View Controller object over from the bottom right to the Tab Bar Controller and place it after the second View Controller. Then bring a Tab Bar Item and place it underneath the View Controller Object you just brought over so it looks like this:
Tab Bar Item
Now we are ready to link this item with your view. Click on the third View Controller and then click the Identity Inspector on the right. After this load you can choose from the drop down in the Custom Class section the class you used for the third view. Repeat these steps for the fourth view as well. Then Save, Build and Run. The third tab should look like this:
Third View App
The fourth tab should be nice and green or whatever color you used like this:
Fourth View App
If you want you can edit the names that show up on the tab items by double clicking on them. You can also click on the attributes inspector and the editing the Title text field under bar Item.

————————————————————–
As always, please leave your comments on iPhone applications, development, or any questions you might have.  Lets Get Started!

reference

Connecting to a SOAP Objective-C

http://sudzc.com/
- It will convert WSDL to SOAP client in Objective-C.

http://csoap.sourceforge.net/
- a SOAP library that depends on libxml2 (which is included in the iPhone SDK)



Hello world for iPhone Developer


Step 1: Download XCode and the SDK

The first thing we need to do is to download the development tools namely XCode and the SDK. To download either of these you will need to register for the Apple ID and Apple developer Connection (ADC). Install both these tools, the procedure to do so is fairly trivial so I am not going into details.
 


Step 2: Create a new Project

Now we have the tools we need to start coding but before we do we need to create a new project.Select File > New Project orApple Key + Shift + N to bring up the new project menu. Select the Applications item of theIPhone OS section from the menu on the left, and select View Based Application from the icons on the right. When prompted enter a project name, I have used HelloWorld in the sample code.


When you create a project you will see a window similar to the one shown in the image on the right. There are four files in the Classes package;
HelloWorldAppDelegate.h
HelloWorldAppDelegate.m
HelloWorldViewController.h
HelloWorldViewController.m

We do not need to change the HelloWorldAppDelegate but just for information I would like to highlight some important parts of the auto-generated code.
The delegate header file contains a reference to a UIWindow object (Line 14) which controls all the user interaction for this application and will manage all other interface components. It also contains a reference to HelloWorldViewController (Line 15) which we will see later manages our one sample view.

In the source file for the application delegate (HelloWorldAppDelegate.m) we find an auto generatedapplicationDidFinishLaunching method. This, as the name suggests, invoked when the application has been loaded and in this method we will add our HelloWorldViewController object to the UIWindow and make it visible;
22    [window addSubview:viewController.view];
23    [window makeKeyAndVisible];  

The IPhone SDK UI follows the Model View Controller (MVC) design pattern and hence each view  has a corrsponding View Controller Object. XCode has automatically created a view for us, (its defined in the HelloWorldViewController.xib file you can see it by double clicking it in the side menu) and linked it to HelloWorldViewController. We now need to add controls to the view handle the generated events. Note we can add controls via a GUI interface called the Interface Builder but I am using the programmatic way for this tutorial. 


Step 3: Adding Controls and Handler to header file


Open the HelloWorldViewController.h file and within the curly braces delinting the HelloWorldViewController interface declaration add the following lines;

IBOutlet UIButton *button;
IBOutlet UILabel *label;
IBOutlet UIView *myView;


We are defining a view to display and a button and label to go in the view. After the curly braces add a method declaration to accept the click event of the button and also properties to access the  UI elements; myButton, myLabel and myView. Your entire code should look something like this:

1    #import <UIKit/UIKit.h>
2
3    @interface MyHelloWorldViewController : UIViewController 
4    {
5
6        IBOutlet UIButton *button;
7        IBOutlet UILabel *label;
8        IBOutlet UIView *myView;
9    }
10
11    -(IBAction)handleEvent:(id)sender;
12    @property (nonatomic, retain) UIButton *button;
13    @property (nonatomic, retain) UILabel *label;
15    @property (nonatomic, retain) UIView *myView;
16
17    @end


Step 4: Adding Controls


First add a synthesize the three UI elements to create the getter and setters. Add the following two lines of code after the @implementation HelloWorldViewController command in line 11 of HelloWorldViewController.m file. 

@synthesize button;
@synthesize label;
@synthesize myView;

Now find the comment saying "Implement loadView if you want to create a view hierarchy programmatically." and un-comment the loadView method that follows this line. Add the following lines to the method to create a button and label;
1    - (void)loadView 
2    {
3    // create and configure the view
4    CGRect cgRct = CGRectMake(0.0, 0.0, 480, 320); //define size and position of view 
5    myView = [[UIView alloc] initWithFrame:cgRct]; //initialize the view    
6    myView.autoresizesSubviews = YES;              //allow it to tweak size of elements in view 
7    self.view = myView;        

8    
// create a UIButton (UIButtonTypeRoundedRect) and play arround with settings
9    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
10   button.frame = CGRectMake(100, 100, 100, 50); // size and position of button
11   [button setTitle:@"Add" forState:UIControlStateNormal];
12  button.backgroundColor = [UIColor clearColor];
13  button.adjustsImageWhenHighlighted = YES;     

14  
//Add action handler and set current class as target
15  [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

16  //Display Button
17  [self.view addSubview:button];
        
18  //create a label
19  cgRct = CGRectMake(100, 170, 100, 50); //define size and position of label
20  label = [[UILabel alloc] initWithFrame:cgRct];
21  label.text = @"Hello World";

22  //Display Label
23  [self.view addSubview:label];
24  }


Click the Build and Go button to check your interface is drawn correctly It should look something like to screen shot below.
 


Step 5: Handling Actions



We have already told the button to send an event (action) to the current class (self) in case of anUIControlEventTouchUpInside event but there is no handler defined to accept the event. We define a handler method in the class by adding the method shown below. It is doing two things, changing the text property of the label and adding a message to the log file.  
 
1    -(void)action:(id)sender
2    {
3        NSLog(@"UIButton was clicked");
4        label.text = @"Bye World";
5    }


Now click the Build and Go button again and click the button. The text of the label should change.


Reference
-http://www.tuaw.com/2009/04/27/iphone-dev-101-the-hello-world-app/

Friday, March 30, 2012

Run a method in a separate thread in Objective-C


In some situations, performing operations in a single thread can cause an application to appear as if it is hanging. For example, when searching with autocomplete enabled within a large amount of records in a database .

Searching with autocompletion enabled
The user is typing and the application is performing a computationally complex operation at the same time (searching within a huge amount of records). By dividing keyboard handling and searching into separate threads, the application will not appear as if it is hanging. Keyboard handling and searching can then be performed ‘simultaneously’.
The following snippets demonstrate the execution of a method within a separate thread.
The initial call
?
1
2
// Perform search in a separate thread
[NSThread detachNewThreadSelector:@selector(performSearch:) toTarget:self withObject:nil];
The method
?
1
2
3
4
5
6
7
8
-(void)performSearch:(id)sender {
    NSAutoreleasePool *autoReleasePool = [[NSAutoreleasePool alloc] init];
    // Perform a search operation here
    [NSThread exit];
    [autoReleasePool release];
}
This entry was posted in Objective-C and tagged ,. Bookmark the permalink. Trackbacks are closed, but you can post a comment.
references
-http://snipd.net/run-a-method-in-a-separate-thread-in-objective-c-cocoa-framework