Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-16-2009, 11:12 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 9
andreasb is on a distinguished road
Question Upon click - randomly show message?

Hello!
I`m trying to create an application which;

- Has 1 button named "Click me!".
- Has 1 empty label.

When the "Click me!" button is pressed, the empty label should randomly show 1 message out of a list with 20 messages.

How can this be done?
And is it possible to make the user be able to click the button only twice pr. day? So that the user can only view 2 messages pr. 24 hours?

Didn`t know whether I should post this here or in the Tutorial Request, but I took a chance .

Thanks a lot !
andreasb is offline   Reply With Quote
Old 08-16-2009, 04:03 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 9
andreasb is on a distinguished road
Default

Just found out another way to do this.
With NSArray you can make a list with several phrases and have them "randomly" printed to a label.
How do I do this?
andreasb is offline   Reply With Quote
Old 08-16-2009, 04:27 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 186
rywola is on a distinguished road
Default

Quote:
Originally Posted by andreasb View Post
Just found out another way to do this.
With NSArray you can make a list with several phrases and have them "randomly" printed to a label.
How do I do this?
Well, I am assuming you want it to show more then the phrase and have an action occur accordingly with the label (like I just did) - in which case you would want each action to be defined seperately and just include the label text change. Even if you don't want to NOW, I think this may be a better way because as you excel more you may want to include actions with this randomicity.
Code:
@interface{
IBOutlet UILabel *mylabel
}
-(IBAction)switchlabela:(id)sender;
-(IBAction)switchlabelb:(id)sender;
-(IBAction)randomswitchlabel:(id)sender;
Then, in the implementation - define the action accordingly:
Code:
-(IBAction)switchlabela:(id)sender
{
[mylabel.text = @"phrasea"];
//rest of action b
}
-(IBAction)switchlabelb:(id)sender
{
[mylabel.text = @"phraseb"];
//rest of action b
}
-(IBAction)randomswitchlabel:(id)sender
{
	NSLog(@"randomswitchlabel");
	NSArray *randomlabelarray = [NSArray arrayWithObjects: @"switchlabela:", @"switchlabelb:", nil];
	int randomnumber = arc4random()%[randomlabelarray count];
	NSString *randomaction = [randomlabelarray objectAtIndex: randomnumber];
	SEL randomselector = NSSelectorFromString(randomaction);
	[self performSelector:randomselector withObject:nil];
}
i learned this courtesy of this forum as well.
rywola is offline   Reply With Quote
Old 08-16-2009, 05:00 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 9
andreasb is on a distinguished road
Default

Quote:
Originally Posted by rywola View Post
Well, I am assuming you want it to show more then the phrase and have an action occur accordingly with the label (like I just did) - in which case you would want each action to be defined seperately and just include the label text change. Even if you don't want to NOW, I think this may be a better way because as you excel more you may want to include actions with this randomicity.
Code:
@interface{
IBOutlet UILabel *mylabel
}
-(IBAction)switchlabela:(id)sender;
-(IBAction)switchlabelb:(id)sender;
-(IBAction)randomswitchlabel:(id)sender;
Then, in the implementation - define the action accordingly:
Code:
-(IBAction)switchlabela:(id)sender
{
[mylabel.text = @"phrasea"];
//rest of action b
}
-(IBAction)switchlabelb:(id)sender
{
[mylabel.text = @"phraseb"];
//rest of action b
}
-(IBAction)randomswitchlabel:(id)sender
{
	NSLog(@"randomswitchlabel");
	NSArray *randomlabelarray = [NSArray arrayWithObjects: @"switchlabela:", @"switchlabelb:", nil];
	int randomnumber = arc4random()%[randomlabelarray count];
	NSString *randomaction = [randomlabelarray objectAtIndex: randomnumber];
	SEL randomselector = NSSelectorFromString(randomaction);
	[self performSelector:randomselector withObject:nil];
}
i learned this courtesy of this forum as well.
Thanks for this!
All I want is to be able to push a button and get a randomized phrase from a list I´ve made.



I get this when I try it .
andreasb is offline   Reply With Quote
Old 08-16-2009, 05:17 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 186
rywola is on a distinguished road
Default

Quote:
Originally Posted by andreasb View Post
Thanks for this!
All I want is to be able to push a button and get a randomized phrase from a list I´ve made.



I get this when I try it .
Are you sure you added the 3 actions into the header?
rywola is offline   Reply With Quote
Old 08-16-2009, 05:20 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 186
rywola is on a distinguished road
Default

Quote:
Originally Posted by andreasb View Post
Thanks for this!
All I want is to be able to push a button and get a randomized phrase from a list I´ve made.



I get this when I try it .
Are you sure you added the 3 actions into the header?

*try replacing the "mylabel.text:"s with "mylabel.text = "s

**or: mylabel.text = [NSString stringWithFormat:@"phrase"];

I ALMOST GAURANTEE that one of those 2 things should work.
rywola is offline   Reply With Quote
Old 08-16-2009, 05:27 PM   #7 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 9
andreasb is on a distinguished road
Default



"Are you sure you added the 3 actions into the header?":

What do you mean by that?
andreasb is offline   Reply With Quote
Old 08-16-2009, 05:31 PM   #8 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 186
rywola is on a distinguished road
Default

Quote:
Originally Posted by andreasb View Post


"Are you sure you added the 3 actions into the header?":

What do you mean by that?
well, in the [header] .h file (under classes), make sure you have the 3 actions that i wrote in the original response to go into the [header] .h. I do not believe that is the problem after looking at your code though, it looks like you miscopied some of what i wrote: i.e. you wrote mylabel:@"phrasea" and mylabel:@"phraseb" instead of mylabel = @"phrasea" and mylabel = @"phrase b". that should now work if you hook up the randomaction to the button.


*remove the brackets. that should finish 'er off.
rywola is offline   Reply With Quote
Old 08-16-2009, 05:32 PM   #9 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

Watch out for your Objective-C syntax, man:

[mylabel.text=@"phrasea"];
should be written as
mylabel.text = @"phrasea";
or
[mylabel setText:@"phrasea"];
kelvinkao is offline   Reply With Quote
Old 08-16-2009, 05:36 PM   #10 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 9
andreasb is on a distinguished road
Default

kelvinkao:

Thank you! That fixed everything .
andreasb is offline   Reply With Quote
Old 08-16-2009, 05:38 PM   #11 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 186
rywola is on a distinguished road
Default

Quote:
Originally Posted by andreasb View Post
kelvinkao:

Thank you! That fixed everything .
just so you know, i mentioned that removal of brackets a few posts ago no worries tho... just don't skim
rywola is offline   Reply With Quote
Old 08-16-2009, 05:41 PM   #12 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

Quote:
Originally Posted by rywola View Post
just so you know, i mentioned that removal of brackets a few posts ago no worries tho... just don't skim
Haha, yeah, I noticed that too after I posted. Your reply was one minute earlier, hehe.
kelvinkao is offline   Reply With Quote
Old 08-16-2009, 05:42 PM   #13 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 186
rywola is on a distinguished road
Default

Quote:
Originally Posted by kelvinkao View Post
Haha, yeah, I noticed that too after I posted. Your reply was one minute earlier, hehe.
aha, well I guess you came in at the right time to steal my glory haha
rywola is offline   Reply With Quote
Old 08-16-2009, 05:54 PM   #14 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

Quote:
Originally Posted by rywola View Post
aha, well I guess you came in at the right time to steal my glory haha

You don't snooze, you lost.
kelvinkao is offline   Reply With Quote
Old 08-16-2009, 05:57 PM   #15 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 9
andreasb is on a distinguished road
Default

Haha, thanks to you both!
And Rywola, without your post I would never be able to make this! ˆˆ,
andreasb is offline   Reply With Quote
Old 08-16-2009, 05:59 PM   #16 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 186
rywola is on a distinguished road
Default

Quote:
Originally Posted by andreasb View Post
Haha, thanks to you both!
And Rywola, without your post I would never be able to make this! ˆˆ,
yeah, no problem. keep in mind you can add any actions to those two phrase threads to get a little more advanced or add more actions/labels if you follow the 'pattern'
rywola is offline   Reply With Quote
Old 08-16-2009, 06:01 PM   #17 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 9
andreasb is on a distinguished road
Default

Yeah! Thanks !
I´ll start by adding more phrases.

Thanks a lot, both of you!
andreasb is offline   Reply With Quote
Reply

Bookmarks

Tags
button, click, label, random, randomly

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 310
12 members and 298 guests
chiataytuday, coolman, givensur, glenn_sayers, guusleijsten, jbro, mediaspree, mottdog, mtl_tech_guy, Punkjumper, vilisei, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:24 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0