Archive

Archive for August, 2011

IPhone App Dev. Tutorial 1 : Simple Interaction with Button and Text

August 21, 2011 2 comments

This tutorial show simple interaction with button and text. I used XCode 4.

1. Create New Project > View-based Application

2. Type Product Name, here i typed “ButtonAndText”

3. Click file “ButtonAndTextViewController.xib”

4. Drag and Drop UIButton from Object Library to View, and Type button title, for example “OK”

5. Open header file “ButtonAndTextViewController.h”, and add following code (just add the bold text) :

#import <UIKit/UIKit.h>

@interface ButtonAndTextViewController : UIViewController {
}
-(IBAction) btnClicked:(id) sender;
@end

6. Open implementation file “ButtonAndTextViewController.m” and type following code after “@implementation ButtonAndTextViewController” :

-(IBAction) btnClicked:(id) sender {
 NSString *str = [[NSString alloc] 
           initWithFormat:@"Welcome Mr. %@ ", textName.text];
 UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Welcome"
                      message:@"Welcome Mr. Tonny"
                      delegate:self cancelButtonTitle:@"OK"
                      otherButtonTitles:nil];
 [alert show];
 [alert release];
}

7. Click file “ButtonAndTextViewController.xib”

8. While pressing Control-Key, Click on the “OK” Button and drag to the “File’s Owner”, then select “btnClicked”

9. Then try running the program, the output should like this :

10. Let’s add  Label and Text Field to View by drag and drop them from Objects Library, then add :

#import <UIKit/UIKit.h>

@interface ButtonAndTextViewController : UIViewController {

   IBOutlet UITextField *textName;

}

@property (nonatomic,retain) UITextField *textName;

-(IBAction) btnClicked:(id) sender;
@end

11. Open implementation file “ButtonAndTextViewController.m” and type following code after “@implementation ButtonAndTextViewController” :

@synthesize textName;

12. Change code for function “-(IBAction) btnClicked:(id) sender”

-(IBAction) btnClicked:(id) sender
{
NSString *str = [[NSString alloc] initWithFormat:@"Welcome Mr. %@ ", textName.text];
UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Welcome"
                          message:str
                          delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    [str release];
 }

13. While pressing Control-Key, Click on the “File’s Owner”  and drag to the UITextField, then select “textName”

14. Run the program, Try to type something in UITextField then click “OK” button to test. The output should like this :

Note :

  • “@property (nonatomic, retain) UITextField *textName;” means that you want the textName outlet to be expose as a property
  • IBAction Identifier is used to synchronize action methods
  • @synthesize keyword indicate that compiler will create getter and setter for the specific property.
  • The nonatomic keyword indicate that there is no need to ensure that the property is used in a thread-safe manner because it is not used in multiple threads.

I Also include the video how to do this in my facebook.

Advertisements
Categories: Iphone, Mac Os, Programming