Sunday, September 11, 2011

Implmenting the UIPickerView



Ok, things are going pretty well just now. I gotten the real basic functionality of the app working, and now I can either start working on making the quiz screen cleaner, or start setting up the functionality of the "setup" page. Actually, I want the setup page to allow the ability to either branch off into the quiz portion or a settings display, where the user can do things like set the JLPT level, the start and ending quiz numbers, and so forth.

I think I'll move into that one first, because I'd like to be able to control the questions that the app is asking. They say the first thing to do is set up the interface and the implementation code, so I'll tackle that first for the setting view controller.

I don't really know enough about the UI controls - a combo selection would be good. The UIPicker seems to be a good choice. That way I can avoid all the validation logic, well at least check then number's between 1 through 5.

Following this tutorial:

http://www.roseindia.net/tutorial/iphone/examples/UIPickerView/UIPickerViewExample.html

It looks like I should implement these:




Then add these in the header:

IBOutlet UILabel *mlabel;
NSMutableArray *arrayNo;
IBOutlet UIPickerView *pickerView;


You can just follow along like I am, it's just adding the normal stuff, properties, synthesize.

Then make the viewDidLoad look like this:

- (void)viewDidLoad
{
[super viewDidLoad];
arrayNo = [[NSMutableArray alloc] init];
[arrayNo addObject:@" 100 "];
[arrayNo addObject:@" 200 "];
[arrayNo addObject:@" 400 "];
[arrayNo addObject:@" 600 "];
[arrayNo addObject:@" 1000 "];

[pickerView selectRow:1 inComponent:0 animated:NO];
mlabel.text= [arrayNo objectAtIndex:[pickerView selectedRowInComponent:0]];
}


Then add these methods:


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
mlabel.text= [arrayNo objectAtIndex:row];
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arrayNo count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [arrayNo objectAtIndex:row];
}




Ok, the trick was to make sure the settings controller view in the nib connected to the file owner, the outlet there. That's why it needed the outlet.

Ok, let's try it.


Bingo. We're in:


U

No comments:

Post a Comment