Τρίτη, 22 Ιανουαρίου 2013

ios pickerView - How to add one pickerview for many textfields

basic thinks:
You must follow the pickerView delegate.
You need to add pickerView with data similar to tablerow delegate.
You can add pickerView.view as inputView of one textfield.
You adding a Toolbar with a done button

1. Create pickerView at xib. Place it outside of the main view and connect its Delegates and source with ctrl+mouse with your File's Owner. Also make a property of him.

2. at .h file of your viewController

@class PKAPeople;
@interface ApartmentsDetailViewController : UIViewController <UITabBarDelegate, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>{
    NSMutableArray * pickerViewData;
}

3. At viewDidLoad make the data source of your pickerview
pickerViewData   = [[NSMutableArray alloc]init];
    for (int i=0;i<=1000;i++)
        [pickerViewData addObject:[[NSString alloc]initWithFormat:@"%i",i]];

4. Make the pickerView inputView of yours textfields
self.xiliostaKoinoxriston.inputView = self.pickerView;
    self.xiliostaAnsanser.inputView = self.pickerView;
    self.xiliostaAllo.inputView = self.pickerView;

5. Create the toolbar for aesthetics and control management. Be aware to make the proper selector below methode if you don't want crash. Also note the CGRectMake is not variables and in this case doesn't support view rotation.  
   UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:
    CGRectMake(0,0, 320, 44)]; //should code with variables to support view rotation
    UIBarButtonItem *doneButton =
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                  target:self action:@selector(patithikeDoneStoPickerView)];
    [myToolbar setItems:[NSArray arrayWithObject: doneButton] animated:NO];


6. add the toolbar at yours textViews
self.xiliostaKoinoxriston.inputAccessoryView = myToolbar;
self.xiliostaAnsanser.inputAccessoryView = myToolbar;
self.xiliostaAllo.inputAccessoryView = myToolbar;

7. Implement the pickerView protocol
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    return 1;
}

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

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [[NSString alloc] initWithFormat:@"%@",[pickerViewData objectAtIndex:row]];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
       if (self.xiliostaKoinoxriston.isFirstResponder)
        self.xiliostaKoinoxriston.text = [pickerViewData objectAtIndex:row];
    if (self.xiliostaAnsanser.isFirstResponder)
        self.xiliostaAnsanser.text = [pickerViewData objectAtIndex:row];
    if (self.xiliostaAllo.isFirstResponder)
        self.xiliostaAllo.text = [pickerViewData objectAtIndex:row];
}

8. Implement the done button selection method so to resign the "modular" keyboard you just made. Note that i search WHO is the firstresponder and HIM resigns. Otherwise a pickerView will stuck on you screen :) 
- (void) patithikeDoneStoPickerView
{
    if (self.xiliostaKoinoxriston.isFirstResponder)
        [self.xiliostaKoinoxriston resignFirstResponder];
    if (self.xiliostaAnsanser.isFirstResponder)
        [self.xiliostaAnsanser resignFirstResponder];
    if (self.xiliostaAllo.isFirstResponder)
        [self.xiliostaAllo resignFirstResponder];
    
}



Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου