Display Message in Iphone

Display message, its very easy to create. You just type your firstname and lastname and click the button then message willl be display on your screen.

Display message, its very easy to create. You just  type your firstname and lastname and click the button then message willl be display on your screen.

Step 1: Create a new project in Xcode using View base application.

Step 2: Add some code in the header file:

#import <UIKit/UIKit.h>

@interface HelloWorld1ViewController : UIViewController {
       
        IBOutlet UITextField *FirstName;
        IBOutlet UITextField *LastName;
        IBOutlet UILabel *lblMessage;
}
- (IBAction) Click:(id)sender;

Step 3: Double click your .xib file and open it to Interface Builder. Click your View icon open the view window. Drag TextField  from the library and place it to view window. Then bring up attribute inspector, type “Enter your First Name”  in the placeholder. Do it once more time and  type “Enter Your Last Name”. Place Round Rect in the View window and give the name “Click”. Next drag Label from the library  and place it to view window. Now connect File’s Owner to the first TextField and select FirstName. Do it once more time and select LastName. Connect File’s Owner to the label and select ibiMessage. Select Round Rect and bring up connection inspector, drag TouchUpInside to File’s Owner and select Click: action. Now save your nib file and go back to the Xcode.

Step 4: In the view base class, we mention a method Click:. When we press the click button it shows the message.

- (IBAction) Click:(id)sender {
       
 NSString *firstName = FirstName.text;
NSString *lastName = LastName.text;
NSString *Message = nil;
       
if([firstName length] == 0 && [lastName length] == 0)
Message = [[NSString alloc] initWithFormat:@"Anonymous says Have A Nice Day!!!"];
else if ([firstName length] > 0 && [lastName length] ==0)
Message = [[NSString alloc] initWithFormat:@"%@ says Have A Nice Day!!!", firstName];
else if ([firstName length] == 0 && [lastName length] == 0)
Message = [[NSString alloc] initWithFormat:@"%@ says Have A Nice Day!!!", lastName];
else
 Message = [[NSString alloc] initWithFormat:@"%@ %@ says Have A Nice  Day!!!", firstName, lastName];
       
lblMessage.text = Message;

[Message release];
}

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

Step 5: Now compile your application and run on the Simulator.(see figure 1)

Figure 1: Message Display.

You can downloaded SourceCode from here HelloWorld1 2

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG