Question

One of Three Search Pages in my app that run off of th Class SearchViewController

I have three different search pages all running off of my Search View Controller. Each one has a different search method, but the screens are basically the same. selections from the picker will fill in the text fields and the lat and long is the device's which is constantly updated and shown in those fields. My question is, to adequately add the correct data and to comply with the UIPickerViewDelegate do I need to add three more classes to run those picker views or is it allowable to do the work in the SearchViewController class to define the PickerView?

Was it helpful?

Solution

It is allowable (and probably preferable) to make the SearchViewController the delegate of each separate UIPickverView. Remember to include the <UIPickerViewDelegate> annotation on your SearchViewController's interface (like @interface SearchViewController : UIViewController <UIPickerViewDelegate>) and the compiler will helpfully remind you if you're missing any required methods; however as of 6.1 there are no required methods in the UIPickerViewDelegate protocol.

Note that every method in the UIPickerViewDelegate protocol takes a UIPickerView as its first argument. Check that argument to see if it is equal to each of your three separate UIPickerViews (and it should certainly be equal to one of them!) and modify the behavior of the delegate methods as needed for each view. You'll end up with code that like this:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (pickerView == self.myFirstPickerView) {
        return self.myFirstSearchMethodResultTitles[row];
    } else if (pickerView == self.mySecondPickerView) {
        return self.mySecondSearchMethodResultTitles[row];
    } else if (pickerView == self.myThirdPickerView) {
        return self.myThirdSearchMethodResultTitles[row];
    } else {
        NSAssert(NO, @"Should not have reached this point!");
        return nil;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top