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 Tools & Utilities

Reply
 
LinkBack Thread Tools Display Modes
Old 10-19-2011, 09:07 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 8
Lindberg is on a distinguished road
Default 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
Lindberg is offline   Reply With Quote
Old 10-22-2011, 05:56 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default

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];
QuantumDoja is online now   Reply With Quote
Old 10-22-2011, 08:52 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: UK
Posts: 82
TTStu is on a distinguished road
Default

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
TTStu is offline   Reply With Quote
Old 10-22-2011, 01:03 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 22
Krillere is on a distinguished road
Default

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.
Krillere is offline   Reply With Quote
Old 10-24-2011, 12:10 PM   #5 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 8
Lindberg is on a distinguished road
Default

Thanks for all the replies!
I hope that my app will work soon!
Lindberg is offline   Reply With Quote
Old 10-28-2011, 04:40 PM   #6 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 8
Lindberg is on a distinguished road
Default

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...
Lindberg is offline   Reply With Quote
Old 10-29-2011, 07:23 AM   #7 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default

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;
}
QuantumDoja is online now   Reply With Quote
Old 10-29-2011, 09:37 AM   #8 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 8
Lindberg is on a distinguished road
Default

Quote:
Originally Posted by QuantumDoja View Post
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.
Lindberg is offline   Reply With Quote
Old 10-29-2011, 09:42 AM   #9 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default

Have you cleaned your project, and are you definitely sure you're building the correct target?
QuantumDoja is online now   Reply With Quote
Old 10-29-2011, 10:13 AM   #10 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 8
Lindberg is on a distinguished road
Default

Quote:
Originally Posted by QuantumDoja View Post
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
Lindberg is offline   Reply With Quote
Old 10-29-2011, 02:49 PM   #11 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 22
Krillere is on a distinguished road
Default

You should try creating a new project. Something might have gone wrong with the 'old' one. :-)

- BTW, are you by any chance Danish?
Krillere is offline   Reply With Quote
Old 10-29-2011, 04:57 PM   #12 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 8
Lindberg is on a distinguished road
Default

Quote:
Originally Posted by Krillere View Post
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?
Lindberg is offline   Reply With Quote
Old 10-30-2011, 04:16 AM   #13 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: UK
Posts: 82
TTStu is on a distinguished road
Default

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...
TTStu is offline   Reply With Quote
Old 10-30-2011, 04:17 AM   #14 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: UK
Posts: 82
TTStu is on a distinguished road
Default

Presuming your app delegate is called AppDelegte, otherwise use the correct name...
TTStu is offline   Reply With Quote
Old 10-30-2011, 08:06 AM   #15 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 22
Krillere is on a distinguished road
Default

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.
Krillere is offline   Reply With Quote
Old 10-30-2011, 10:06 AM   #16 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 8
Lindberg is on a distinguished road
Default

Quote:
Originally Posted by TTStu View Post
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..
Lindberg is offline   Reply With Quote
Old 10-31-2011, 05:06 AM   #17 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 8
Lindberg is on a distinguished road
Default

Quote:
Originally Posted by Lindberg View Post
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!
Lindberg is offline   Reply With Quote
Reply

Bookmarks

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: 409
8 members and 401 guests
7twenty7, mer10, QuantumDoja, Retouchable, RobTaku, SLIC, tim0504, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,925
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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