2010年5月12日水曜日

UIPickerViewDelegate Protocol

UIPickerViewを実装する際は、UIPickerViewControllerに、UIPickerViewDelegateとUIPickerViewDataSorceプロトコルをコントローラで指定して実装します。
ピッカービューの方向を設定する
– pickerView:rowHeightForComponent:
– (CGFloat)pickerView:(UIPickerView)pickerView widthForComponent:(NSInteger)component
コンポーネントで指定されたピッカービューの幅を返す
コンポーネントのデータ(行)の内容を設定する
このコンポーネントは @optionalとされていますが、ピッカービューを使用するときはコンポーネントの行の中身を提供するために下記のメッソッドの、いずれかを実装する必要があります。
– pickerView:titleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView:
ピッカー動作の度に応答する
– (void)pickerView:(UIPickerView)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
ピッカーが動く度に呼び出されるメソッドです。応用としては1つのコンポーネントに連動してもう1つのコンポーネント値を変えるために、当該ピッカーのコンポーネント内の値を変化させ、リロードするものがあります。
  • 例:
  • -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
  • if (component == kStateComponent) {
  • NSString *selectedState = [self.states objectAtIndex:row];
  • NSArray *array = [stateZips objectForKey:selectedState];
  • self.zips = array;
  • [picker selectRow:0 inComponent:kZipComponent animated:YES];
  • [picker reloadComponent:kZipComponent];
  •  }
  • }

0 件のコメント:

コメントを投稿