In this application we will see how to messages randomly display using button pressed. This is the very simple example , so let see how it will be display.
Step 1: Create a View base application using template. Give the application name “RandomMessage”.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.
Step 3: Xpand classes and notice Interface Builder created the RandomMessageViewController class for you. Expand Resources and notice the template generated a separate nib, RandomMessageViewController.xib, for the “RandomMessage”.
Step 4: Open the RandomMessageViewController.h file and added UITextView for display text and UIButton , add two methods. So make the following changes in the file.
@interface RandomMessageViewController : UIViewController {
IBOutlet UITextView *userText;
IBOutlet UIButton *button;
}
@property(nonatomic,retain) IBOutlet UITextView *userText;
@property(nonatomic,retain) IBOutlet UIButton *button;
-(IBAction) changeText:(id)sender;
-(void)updateText;
Step 5: Double click the RandomMessageViewController.xib file and open it to the Interface Builder. First drag the Round Rect Button from the library and place it to the view window and give the name “PressMe!!!” , drag the Text View from the library and place it to the view window and delete the text of the textview. Select the PressMe button and bring up Connection Inspector and drag from the TouchUpInside to the File’s Owner icon and select changeText: action. Next drag from the File’s Owner icon to the PressMe button and select button. Connect File’s Owner icon to the View icon and select view and connect File’s Owner icon to the TextView and select useText.You have done all the connection, now save the RandomMessageViewController.xib file, close it and go back to the Xcode.
Step 6: In the RandomMessageViewController.m file, make the following changes:
-(IBAction) changeText:(id)sender
{
y++;
if (y>8) y=0;
[self updateText];
}
- (void)updateText {
switch (y) {
case 0: [userText setText:@"Hi"]; break;
case 1: [userText setText:@"Hello"]; break;
case 2: [userText setText:@"Hello World"]; break;
case 3: [userText setText:@"iPhone"]; break;
case 4: [userText setText:@"iPad"]; break;
case 5: [userText setText:@"iPod"]; break;
case 6: [userText setText:@"Mac Mini"]; break;
case 7: [userText setText:@"Enter Your Name"]; break;
case 8: [userText setText:@"Enter Your Age"]; break;
}
}
Step 7: Now compile and run the application in the Simulator.
You can Download SourceCode from here RandomMessage












Hi, Nice tutorial, but it’s not “random”. Instead of incrementing y, you should just set it to a random number between 0 and n-1, where n is the number of messages in your case statement. You could also store your message strings in an array and use y to index into the array.
sorry, this is wrong it shows a continuous line of messages for random messages use this code
- (void)updateText {
int rNumber = rand() % 8;// divides by number of fields to give a perfectly random generation
switch (rNumber) {
case 0: [userText setText:@"Hi"]; break;
case 1: [userText setText:@"Hello"]; break;
case 2: [userText setText:@"Hello World"]; break;
case 3: [userText setText:@"iPhone"]; break;
case 4: [userText setText:@"iPad"]; break;
case 5: [userText setText:@"iPod"]; break;
case 6: [userText setText:@"Mac Mini"]; break;
case 7: [userText setText:@"Enter Your Name"]; break;
case 8: [userText setText:@"Enter Your Age"]; break;
default: @”Apple”;
break;
}
}
Thanks for your comment!!! I have upload updated SourceCode..
Great article… I need some help on step5…Could you send me email or contact information?