views:

158

answers:

2

Dear Developers,

I am developing an iphone app that has a single View containing A UILabel. The UILabel displays strings from within a plist that is structued as follows;

Root................................................(Array)
.............Item 0.................................(Dictionary)
.........................Question...................(String)    "question 0"
.............Item 1.................................(Dictionary)
.........................Question...................(String)    "question 1"
.............Item 2.................................(Dictionary)
.........................Question...................(String)    "question 2"

The Plist is defined as an NSArray and the strings within each Dictionary are called using Constant Keys.

I now want to implement a UIButton that, when pressed, loads the next item from the plist/array into the UILabel.

I need to know specifically what this Action would look like in code because i can't find an example anywhere :(. Can anybody help with this?

j

A: 

Sounds like you want a UITabelView and to set up each cell as an item from your property list. This is very well documented, not only in terms of the APIs that will be required in this case, but in sample code. The table view suite for instance, is one great place to look.

Try and visualize your use case in terms of a UI, you'll see what I mean.

jer
Hi Jer, Thanks for your response. The view i want to use is a simple UIVIew with a single UIlable. The button needs to navigate up and down the PList items and display their response. not a tableview. Any ideas?
james coleman
Sure you can do that fairly easily. Create a custom view with a label and a button (position to your liking) and then in your method which gets called when your button is tapped, update the UILabel. An alternative would be to do away with the button, and make the label respond to tap events. You can use UITapGestureRecognizer if you're targeting 3.2 or 4.0 or the traditional touchesEnded:withEvent:
jer
now that sounds exactly what i need! Do you have an example of how the code would look for that? i can't find examples anywhere :(.j
james coleman
Open up interface builder, create a new view and lay it out how you want. connect up some outlets for your button and your label (only need to hook up an outlet for the button if you want to be able to manipulate it, i.e., hide it, rename it, etc) and then hook up an action to a method on clicking the button. This is basic stuff, well documented in many tutorials. Once you have that, the logic you'd implement in the view controller's click handler would be however you want to fetch your next item, and replace the contents of the label. Then simply add as a subview this custom view
jer
thats fine - i understand everything you have said here. It is the code that allows the action to occur (i.e. to load the next item in the plist/array) that i need to see specifically and id like to see an example explicitly written!
james coleman
A: 

Hmm, I have my own way of doing things like this, and I'll show you how right now.

You'll want to base it off like this. You'll need 4 UIButtons for 4 words, so for 1 word its 1 button. I'll start in the .h by declaring my outlets and actions.

{
IBOutlet UIButton *Button1; 
IBOutlet UIButton *Button2; 
IBOutlet UIButton *Button3; 
IBOutlet UIButton *Button4;
IBOutlet UILabel *Label; 
}
// and now I'll do 4 actions.
-(IBAction)Button1Action;
-(IBAction)Button2Action;
-(IBAction)Button3Action;
-(IBAction)Button4Action;

In my .m, I'll start off with some code that will run immediately after the app launches. So I can just do viewDidLoad, since I can't remember the applicationDidFinish launching method off the top of my head.

-(void)viewDidLoad {
NSLog(@"Application has launched, hiding buttons 2, 3, and 4.");
[Button2 setHidden:YES];
[Button3 setHidden:YES];
[Button4 setHidden:YES];
}

Now we're going to do the actions. Separate the 4 buttons in Interface Builder, connect the buttons, then put the first button where you want it, the second button over it, third, then fourth, and so on for however many words you'll have.

-(IBAction)Button1Action {
NSLog(@"Button 1 pressed.);
[Button2 setHidden:YES];
[Button3 setHidden:YES];
[Button4 setHidden:YES];
[Button1 setHidden:NO];
Label.text = @"word number 1";
}

-(IBAction)Button2Action {
NSLog(@"Button 2 pressed.);
[Button1 setHidden:YES];
[Button3 setHidden:YES];
[Button4 setHidden:YES];
[Button2 setHidden:NO];
Label.text = @"word number 2";
}

-(IBAction)Button3Action {
NSLog(@"Button 3 pressed.);
[Button1 setHidden:YES];
[Button2 setHidden:YES];
[Button4 setHidden:YES];
[Button3 setHidden:NO];
Label.text = @"word number 3";
}

-(IBAction)Button4Action {
NSLog(@"Button 4 pressed.);
[Button1 setHidden:YES];
[Button2 setHidden:YES];
[Button3 setHidden:YES];
[Button4 setHidden:NO];
Label.text = @"word number 4";
}

And then that's pretty much it. I like to do things my way, sometimes its longer than the usual way that people do it, and sometimes its shorter. I hope this helps you.

Daniel Ricany
Thanks for the working - i think this would be perfect if I was only going to use <4 items in the plist. The problem is - i plan to expand this plist (>30 items)once i have the method of to scan through the items with a button.
james coleman