Home > Iphone, Mac Os, Programming > IPhone App Dev. Tutorial 1 : Simple Interaction with Button and Text

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

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
  1. May 12, 2013 at 5:15 pm

    Howdy! Someone in my Facebook group shared this site
    with us so I came to give it a look. I’m definitely loving the information. I’m book-marking and
    will be tweeting this to my followers! Great blog and amazing style and design.

  2. May 19, 2013 at 3:01 am

    In the beginning of the 11th Century the represents of Christianity is able to
    banned the duel form einvigi and holmgangr as lawfully accepted means to
    solve a dispute, and at the same time the berserkr
    and the beserksgangr was made illegal. There are e – Book companies that will format
    your manuscript files into e – Book files and can
    even provide a cover image. A thought in this substance, produces the
    thing that is imaged by the thought.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: