UIPickerView – Creating a simple picker view

Apple provides a control knows as the UIPickerView, with which a user can select an item from a list. In this tutorial you will learn how to create a simple picker view.
Make a new venture by deciding on “Windows-Based Application”, I have known as my venture “PickerView”. I also included another perspective known as “PickerView” and designed a perspective operator for the perspective known as “PickerViewController”. Add a UIPickerView to the perspective and build all the right contacts. The “PickerViewController” tools two methods and this is how the headlines computer file looks like

//RootViewController.h
#import <UIKit/UIKit.h>
 
@interface PickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
 
IBOutlet UIPickerView *pickerView;
NSMutableArray *array;
}
@end
Connect the delegates

This is how the UIPickerView is included to the subview of the screen.
Creating the data sourceLets create a data source, with all the colors in a rainbow. The array is declared in the header file and released in the dealloc method. It is populated in viewDidLoad method and this is how the code looks like
- (void)viewDidLoad {
    [super viewDidLoad];
array = [[NSMutableArray alloc] init];
[array addObject:@"UITableView"];
[array addObject:@"UIView"];
[array addObject:@"UIAlertView"];
[array addObject:@"UIWebView"];
}
The picker perspective can be separated into elements, we need to specify how many elements to present. This is done in numberOfComponentsInPickerView technique. This is how the supply value looks like
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
Now that the picker perspective knows how many elements it should assume, we need to specify how many series it should present. This is done in pickerView:numberOfRowsInComponent. This is how the supply value looks like
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [array count];
}
The method pickerView:titleForRow:forComponent gets called n number of times, where n is the number returned in pickerView:numberOfRowsInComponent. This is how the source code looks like

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

In the above value, we come back the subject at catalog using the row parameter. Here we ignored the element parameter, as we only have one element to existing. If you have more then one element, then we come back the row existing in that given element. Run your software to see the principles in the picker perspective.

Handling Events
The technique pickerView:didSelectRow:inComponent is known as when products is chosen in the picker perspective. In the example, I merely log the chosen coloring and the row variety. This is how the value looks like

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"Selected Color: %@. Index of selected color: %i", [array objectAtIndex:row], row);
}


Conclusion
Picker perspective is the best management to present a product, from which one individual piece can be chosen. In this guide, we saw how simple it is to manage a picker perspective and answer situations. I trust this guide has assisted you in some little way. A lot more for some more lessons on the UIPickerView subject.


0 comments:

Post a Comment

Powered by Blogger.
Twitter Delicious Facebook Digg Stumbleupon Favorites More