Hi, I am a real newbie to programming on iphones. What I am trying to do is basically make a joke app for a school project. It starts with a navigation based interface, for categories of jokes. Once you tap a category, it goes to a view with a label and a button. You tap the button to randomly pick and display a joke (from a text file or something like that).
Because i am very new to this, though, I have mostly been making my app out of tutorials. I already have made my navigation based app; with subviews and all. Tapping a subview gets a view with one round button and a label. However, I have not yet set up the whole random part.
Right now, when I tap the button, it says "set by [whichever category it is in]" because the tutorial (noob, I know) said I should put a code to make it access global application data. I'm still not sure if I need this for my app, I have a feeling it would actually mess it up.
My questions:
Where and how do I construct my text file with all the jokes for a certain category?
What is the code to make it randomly pick a joke from the text, and where do I insert this code?
Do I need the global application data accessing code?
Just create a new PLIST in Xcode. File > New File > Other > Property List File. Name it anything you would like, just change :@"filename" to the name of your file, NOT including ".plist".
Once you begin to edit you will see your Root which is your dictionary (<dict>) . Add as many arrays as you would like.
Change [categoryDic valueForKey:@"items"]; to whatever you name your Array. For example
If the name of your array is "sillyjokes". ' I've uploaded an example PLIST file.
You insert the code I posted before in the action of your button. since the NSString *fileContents is used multiple times its easier to place it in your header file. Same with UILabel *someLabel;
I set up the plist successfully (thanks!!!) but im still having trouble.
I modified the code in your last post so it should work with the plist and array I made, and then I put it in my subviewonecontroller.m. When I tried in the .h like you suggested, it made even more errors.
Here is a screenshot of what happened:
The last error I think I can solve by deleting that part. But, I really need help with the others..they don't make much sense.
First, you cant have multiple definitions of the same method. OnButtonClick is defined twice, you need to combine the two methods. I'll get to that later.
fileContents is an NSString and needs to be declared in your Header file.
Code:
NSString *fileContents;
or if you dont plan on using it in more than one method you dont have to, just add NSString *fileContents = blah blah blah...
You didn't quite finish. You renamed categoryDic, "elevatorJokes" but didn't change it in [categoryDic valueForKey...
Same with your NSMutableArray eleJokes. [items count]; should be [eleJokes count];
[sillyJokes objectAtIndex:randomNumber]; needs to be [eleJokes objectAtIndex:randomNumber];
Now, copy every inside your second OnButtonClick. Paste it inside the first one.
Delete the displayString line.
And edit [appDelegate setModelData:displayString]; to
Code:
[appDelegate setModelData:joke];
I think that about takes care of it...
Also, you will want to give your variables more meaningful names. Such as elevatorJokesDict or eleJokesMutArray. That way in the future you actually know what they are.
Last edited by brandon0104; 04-26-2009 at 07:56 PM.
4 Errors to go, I think 2 can be taken down at once
As you can see in the pic below, once I merged the second "onbuttonclick" function, I got problems with the code below it. It seems having the onbuttonclick code there made it work;now that its merged the stuff below doesnt work. I tried a few things like deleting that piece of code (ondatachangeevent), and that in turn made the stuff below it have errors.
Also, how exactly do I declare joke and elejokes? I do want to use filecontents for more than one thing. I have "NSString *fileContents;" so far, do I do something like NSString *fileContents = etc on the line below it?
I think you're biting off a bit more than you can chew. You need to work through some more tutorials and work on little projects first. This helps a lot, trust me I've been where you are.
1. [appDelegate setModalData: joke]; is placed before joke is even declared. Thats why you're getting that error.
2. joke = [eleJokes object.... is spelled differently than your original elejokes mutable array.
3. UILabel is class, not a variable. As I said in the last post you should delete that line anyway.
Oh, I completely agree. Quite a bit of this is over my head (even though I understand a little :P). Thing is, I don't have enough time to do all those tutorials and such, I just have to jump right in.
I finally figured out how to make this work--I had to rename all the "joke" references to "elejokes". Now, it works.
I really can't say how thankful I am of you You have been very helpful. When I finish the app, im going to make sure to recognize you in the credits. Anyway, thanks again.
4 Errors to go, I think 2 can be taken down at once
As you can see in the pic below, once I merged the second "onbuttonclick" function, I got problems with the code below it. It seems having the onbuttonclick code there made it work;now that its merged the stuff below doesnt work. I tried a few things like deleting that piece of code (ondatachangeevent), and that in turn made the stuff below it have errors.
Also, how exactly do I declare joke and elejokes? I do want to use filecontents for more than one thing. I have "NSString *fileContents;" so far, do I do something like NSString *fileContents = etc on the line below it?
Thanks again, you are being very helpful.
Hey I am new to developing now and was reading this post and was wondering where did you define -elejokes-
I think you're biting off a bit more than you can chew. You need to work through some more tutorials and work on little projects first. This helps a lot, trust me I've been where you are.
1. [appDelegate setModalData: joke]; is placed before joke is even declared. Thats why you're getting that error.
2. joke = [eleJokes object.... is spelled differently than your original elejokes mutable array.
3. UILabel is class, not a variable. As I said in the last post you should delete that line anyway.
4. You didn't even close your brackets. "}"
I am trying to create an app that displays random text and when I use
Code:
[appDelegate setModelData:jokes];
A warning comes up that says my delegate might not respond to "setModelData" what could be the reason for this?
Hey I am new to developing now and was reading this post and was wondering where did you define -elejokes-
NSMutableArray *elejokes = [jokes valueForKey:@"elejokes"];
defined elejokes. i was having a problem with it because I capitalized the J there, it should have been lower case like the others.
Quote:
A warning comes up that says my delegate might not respond to "setModelData" what could be the reason for this?
Its kind of weird, I get that same thing in the same place sometimes (i have no idea why) but it has never prevented the app from running or crashed it or anything like that. so I just ignore it, if you have everything else fixed you should be fine.
EDIT read this-
actually i think you might have something different with the warning, make sure that its like this
int randomNumber = arc4random() % [x count]; x = [x objectAtIndex:randomNumber];
[appDelegate setModelData:x];
}
with y as the name of the plist
and x as the name of the array.
if you make sure it is just like this with x and y replaced it SHOULD NOT have a warning.
NSMutableArray *elejokes = [jokes valueForKey:@"elejokes"];
defined elejokes. i was having a problem with it because I capitalized the J there, it should have been lower case like the others.
Its kind of weird, I get that same thing in the same place sometimes (i have no idea why) but it has never prevented the app from running or crashed it or anything like that. so I just ignore it, if you have everything else fixed you should be fine.
EDIT read this-
actually i think you might have something different with the warning, make sure that its like this
int randomNumber = arc4random() % [x count]; x = [x objectAtIndex:randomNumber];
[appDelegate setModelData:x];
}
with y as the name of the plist
and x as the name of the array.
if you make sure it is just like this with x and y replaced it SHOULD NOT have a warning.
Ok maybe you can tell me what is going on all I did was add everything hear to my application. Whenever I run my application in 2.2.1 and I click on a table row that when touched goes to the second view the app crashes. But when I run the app in 3.0 the app does not crash when I perform the action described above. I think there might be an error because my second view is populated from the app delegate. But if this is the problem then why would it work in 3.0 and not 2.2.1 any ideas?
NSMutableArray *elejokes = [jokes valueForKey:@"elejokes"];
defined elejokes. i was having a problem with it because I capitalized the J there, it should have been lower case like the others.
Its kind of weird, I get that same thing in the same place sometimes (i have no idea why) but it has never prevented the app from running or crashed it or anything like that. so I just ignore it, if you have everything else fixed you should be fine.
EDIT read this-
actually i think you might have something different with the warning, make sure that its like this
int randomNumber = arc4random() % [x count]; x = [x objectAtIndex:randomNumber];
[appDelegate setModelData:x];
}
with y as the name of the plist
and x as the name of the array.
if you make sure it is just like this with x and y replaced it SHOULD NOT have a warning.
Never mind last post for some reason Interface Builder threw in a UiSearchDisplay controller.
I am a noobie and trying to understand the code posted...I think I have a good handle on your code. the thing I don't understand is:
[appDelegate setModelData:x]
Why did it replace [someUILabel setText:joke]?
Is it necessary to use Delegate to work? I am building my own exercise with a label and a button..so that when a button is pushed..a random line will be displayed in the UIlabel.
I have the same Idea as you and am trying to make a joke app! Can you post the final source code of the app so I can download it to see what I did wrong? I keep getting so many errors!
I have the same Idea as you and am trying to make a joke app! Can you post the final source code of the app so I can download it to see what I did wrong? I keep getting so many errors!
I would like to see the source code too.
Quote:
Originally Posted by orange gold
perhaps you could just manual enter all of the jokes through code... it would appear to be easier in my opinion
Code:
NSArray *myranjoke = [NSArray arrayWithObjects:
@"Joke #1 goes here",
@"Joke #2 goes here",
@"Joke #3 goes here,
@"Joke #4 goes here", nil]; // you can add as many as you want.. not just 4
int chosen = arc4random() % [myranjoke count];
nameofUiLabel.text = [myranjoke objectAtIndex: chosen];
It might be easier, but it wouldn't be as neat. It would be easier to edit if you created them from a plist then edited it.
i have done something similar to this, but what i need to know is, is there any way of making it customizable so the user can add/delete there own strings??