How To Show Messages Rusing Button Pressed In iPhone

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.

 

#import <uikit/UIKit.h>

@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:

 

int y = 0;

-(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

Leave a Comment:

11 comments
DJ @ Fermentarium says May 28, 2010

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.

Reply
    Neeraj says January 21, 2011

    /RandomMessage/Classes/HappyRandomMessageViewController.m:156:0 /RandomMessage/Classes/RandomMessageViewController.m:156: warning: property ‘userText’ requires method ‘-userText’ to be defined – use @synthesize, @dynamic or provide a method implementation

    This is how it looks

    – (void)dealloc {
    [super dealloc];
    }

    @end

    Any inputs will be appreciated…

    Reply
Jack says May 29, 2010

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;

}

}

Reply
    Giannis says December 13, 2011

    Hi Jack, your example has also the same results as the first one.

    I have change it to:

    – (void)updateText {

    int rNumber = arc4random() % 8;
    switch (rNumber) {
    case 0: [textView setText:@”1″]; break;
    case 1: [textView setText:@”2″]; break;
    case 2: [textView setText:@”3 “]; break;
    case 3: [textView setText:@”4″]; break;
    case 4: [textView setText:@”5″]; break;
    case 5: [textView setText:@”6”]; break;
    case 6: [textView setText:@”7 “]; break;
    case 7: [textView setText:@”8″]; break;
    case 8: [textView setText:@” 9″]; break;
    break;

    }

    }

    and it works pretty good.

    Reply
Sushant says May 29, 2010

Thanks for your comment!!! I have upload updated SourceCode..

Reply
Neeraj says January 20, 2011

Great article… I need some help on step5…Could you send me email or contact information?

Reply
Rob DeNicola says November 12, 2011

How would one dot his for randomly selected views as in switch the views randomly using a button pressed method? Any assistance would be greatly appreciated.

Thank,

Rob DeNicola

Reply
Cliff Yiu says December 20, 2011

Why I get the same sequence of random number every time I run with ran()?

I’m using Xcode.

Reply
extreme premium garcinia cambogia reviews says October 28, 2013

Hi, I do believe this is an excellent blog.
I stumbledupon it 😉 I may come back yet again since i have book-marked it.
Money and freedom is the greatest way too change, may you be rich and continue
to guiide others.

Reply
    Vishy says November 9, 2013

    Hi

    Thanks for your comment.

    Please do come back and check us out from time to time. We’re starting a new series of blogs this month. We’ll be releasing four blog posts each month covering all aspects of iOS and iPhone development.

    Reply
      Rick says November 11, 2013

      Testing comment

      Reply
Add Your Reply