-
Notifications
You must be signed in to change notification settings - Fork 1
A 1.4. Picking Values with the UIPickerView
rayastar edited this page Feb 7, 2015
·
1 revision
First let’s go to the top of the .m (implementation) file of our view controller and define our picker view:
#import "ViewController.h" @interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate> @property (nonatomic, strong) UIPickerView *myPicker; @property (nonatomic,copy) NSArray *myarray; @end
Now let’s create the picker view in the viewDidLoad method of our view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
self.myPicker = [[UIPickerView alloc] init];
self.myPicker.dataSource = self;
self.myPicker.delegate = self;
self.myPicker.center = self.view.center;
self.myPicker.showsSelectionIndicator = YES;
[self.view addSubview:self.myPicker];
self.myarray = [[NSArray alloc] initWithObjects:@"hello",@"how",@"are",@"you", nil];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if ([pickerView isEqual:self.myPicker])
{
return 1;
}
return 0;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if ([pickerView isEqual:self.myPicker])
{
return [self.myarray count];
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if ([pickerView isEqual:self.myPicker])
{
return [NSString stringWithFormat:@"%@",self.myarray[row]];
}
return nil;
}