UIPickerViewですの
Head First iPhone Developmentを読んでる。
以下はUIPickerViewのメモね。
UIPickerViewで使うプロトコル
UIPickerViewDataSourceプロトコル
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { // コンポーネントはふたつ return 2; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { // それぞれのコンポーネントの行数を返す if (component == 0) { return [activities count]; } else { return [feelings count]; } }
UIPickerViewDelegateプロトコル
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component == 0) { return [activities objectAtIndex:row]; } else { return [feelings objectAtIndex:row]; } }
選択した行の取得
選択した行を取得するには UIPIckerView#selectedRowInComponent: を使う。
- (IBAction)tweetIt { NSString *activity = [activities objectAtIndex: [tweetPicker selectedRowInComponent:0]]; NSString *feeling = [feelings objectAtIndex:[tweetPicker selectedRowInComponent:1]]; NSString *message = [[NSString alloc] initWithFormat: @"I'm %@ now and feeling %@ about it.", activity, feeling]; NSLog(@"%@", themessage); ... }