10-19-2011, 09:07 AM
#1 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 8
Storing strings and show them randomly... SOLVED
SOLVED! Thanks for all the help!
Hi!
First of all, im totally new to this forum. Please don't just answer my question with: SEARCH!
I'm looking for a tutorial or some code pieces to solve my problem.
I'm developing an iOS app in Xcode. I've got two buttons and one label. I want to do this: when one button is pressed, the label will be replaced with a sentence, i want to store many different sentences so that the same won't show up more than twice. There should be another set of sentences showing up if you press the other button.
This is a truth or dare game.
So my question is: how can i store two sets of sentences? How can i make the button show random sentences?
Sorry if my explanation wasn't that good...
Last edited by Lindberg; 10-31-2011 at 02:00 PM .
Reason: Problem is now solved
10-22-2011, 05:56 AM
#2 (permalink )
Registered Member
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
You can use an array, pseudocode:
Code:
array = [Array alloc]
[array addObject:@"first sentence"];
[array addObject:@"second sentence"];
[array addObject:@"third sentence"];
Then in your button press code, again pseudocode:
Code:
int randomNumber = random form 0 to array.length;
lblText.Text = [array objectAtIndex:randomNumber];
10-22-2011, 08:52 AM
#3 (permalink )
Registered Member
Join Date: Aug 2011
Location: UK
Posts: 82
You could also add each phrase to the array twice, and remove each one that you show... hence, each one won't show up more than twice
10-22-2011, 01:03 PM
#4 (permalink )
Registered Member
Join Date: Jul 2011
Posts: 22
If you store the sentences in a text file, do like this:
Code:
NSString *contentString = [NSString stringWithFile:[[NSBundle mainBundle] pathForResource:@"File" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
NSArray *sentences = [contentString componentsSeperatedBy:@"\n"];
int rnd = arc4random() % sentences.count+1;
[label setText:[sentences objectAtIndex:rnd]];
Just wrote it out of my head, but that should do the trick. :-)
Note: You might want to read the NSArray and NSString at app-startup, and then just find the random int when clicked the button.
10-24-2011, 12:10 PM
#5 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 8
Thanks for all the replies!
I hope that my app will work soon!
10-28-2011, 04:40 PM
#6 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 8
Hi again!
I was building my app and got no errors, but in the iOS Simulator my app won't work. It's just black after pressing the icon.
When the iOS Simulator runs Xcode automatically goes to my main.m and shows a green message under this codeline:
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
The green message says: Thread 1: Program received signal: "SIGABRT"
I've searched Google but didn't find anything useful...
10-29-2011, 07:23 AM
#7 (permalink )
Registered Member
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
My main looks just like this:
Code:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
10-29-2011, 09:37 AM
#8 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 8
Quote:
Originally Posted by
QuantumDoja
My main looks just like this:
Code:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
I'm just getting alot of erros about NSAutoreleasePool being unavailable when i'm using that main.
10-29-2011, 09:42 AM
#9 (permalink )
Registered Member
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
Have you cleaned your project, and are you definitely sure you're building the correct target?
10-29-2011, 10:13 AM
#10 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 8
Quote:
Originally Posted by
QuantumDoja
Have you cleaned your project, and are you definitely sure you're building the correct target?
Hmm, how do I clean my project?
And I'm just clicking run in the top left hand corner. Sorry if I ask weird questions, this is my first iOS app
10-29-2011, 02:49 PM
#11 (permalink )
Registered Member
Join Date: Jul 2011
Posts: 22
You should try creating a new project. Something might have gone wrong with the 'old' one. :-)
- BTW, are you by any chance Danish?
10-29-2011, 04:57 PM
#12 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 8
Quote:
Originally Posted by
Krillere
You should try creating a new project. Something might have gone wrong with the 'old' one. :-)
- BTW, are you by any chance Danish?
Okay, will try.
No, but a good guess, I'm Swedish
How could you guess?
10-30-2011, 04:16 AM
#13 (permalink )
Registered Member
Join Date: Aug 2011
Location: UK
Posts: 82
What version are you running of xcode?
4.2/ios5 needs the @autorelease construct that you first used, BUT you must also #import "AppDelegate.h" at the top too.
Older versions, use QuantumDojas code, BUT replace the last nil with @"AppDelegate"
I think the former is what youre after...
10-30-2011, 04:17 AM
#14 (permalink )
Registered Member
Join Date: Aug 2011
Location: UK
Posts: 82
Presuming your app delegate is called AppDelegte, otherwise use the correct name...
10-30-2011, 08:06 AM
#15 (permalink )
Registered Member
Join Date: Jul 2011
Posts: 22
I know someone called Lindberg, and I'm Danish :-)
- Try uploading the project folder (.zip it) and give us the link. Someone might be able to fix it.
10-30-2011, 10:06 AM
#16 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 8
Quote:
Originally Posted by
TTStu
What version are you running of xcode?
4.2/ios5 needs the @autorelease construct that you first used, BUT you must also #import "AppDelegate.h" at the top too.
Older versions, use QuantumDojas code, BUT replace the last nil with @"AppDelegate"
I think the former is what youre after...
I run Xcode 4.2 and AppDelegate.h is "imported" and that is the correct name of my delegate..
10-31-2011, 05:06 AM
#17 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 8
Quote:
Originally Posted by
Lindberg
I run Xcode 4.2 and AppDelegate.h is "imported" and that is the correct name of my delegate..
With a little help from the debugger in came clear that the AdBanner I added to my app didn't work that well, so I removed it for now. Now i got other problems, but i will try to solve them myself before asking here
Thanks for the help anyway!
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,925
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom