fairly new, and I would need a hand understanding UIPickerViews.
I have created a UIPickerView programmatically for my project:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 375, 200)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
}
And then added a method for the number of rows:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 5;
return numRows;
}
Which returns as expected five question marks. I could then go on to create an array to fill those rows etc... but instead I next add another UIPickerView like so:
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 375, 200)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
UIPickerView *my2PickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 400, 375, 200)];
my2PickerView.delegate = self;
my2PickerView.showsSelectionIndicator = YES;
[self.view addSubview:my2PickerView];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 5;
return numRows;
}
Now I have two pickerview controllers and they BOTH have five rows. My question is how do chose to which pickerview the method applies and also could anyone explain why does the method apply to all pickerviews in the project? Thanks.
You only have one delegate method for two PickerViews ; that's something I don't like with iOS but you don't really have a choice here.
You have to if-statement
yourself out of this.
The pickerView
parameter in the delegate method is the pickerview that is being assigned the number of rows.
Note that this is valid for any of the usual delegate methods for iOS, wether it's the numberOfRows of your pickerview, or your tableview, or your collectionView, or any delegate method that has the view in parameter.
The easy understandable way is to have your pickerview as fields of your class (or properties), and simply compare the parameter with it.
@interface ViewController ()
@property (weak, nonatomic) UIPickerView *_mySexyPickerView;
@property (weak, nonatomic) UIPickerView *_myOtherPickerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_mySexyPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 375, 200)];
_mySexyPickerView.delegate = self;
_mySexyPickerView.showsSelectionIndicator = YES;
[self.view addSubview:_mySexyPickerView];
_myOtherPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 400, 375, 200)];
_myOtherPickerView.delegate = self;
_myOtherPickerView.showsSelectionIndicator = YES;
[self.view addSubview:_myOtherPickerView];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView == _mySexyPickerView){
return 2;
}
if (pickerView == _myOtherPickerView){
return 19;
}
return 0;
}