Picker View
- 拉到viewController上面後按右鍵
- datasource , delegate 拉到viewController裡面
- 拉到.m檔案裡面
- .h 加上<UIPickViewDataSource,UIPickViewDelegate>
- 開始寫code
code:
@implementation ViewController { __weak IBOutlet UIPickerView *picker; NSArray *items; NSInteger cityIndex; NSInteger priceIndex; NSInteger typeIndex; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. cityIndex = typeIndex = priceIndex = 0; NSArray *cities = @[@"台北市", @"台中市", @"高雄市", @"台北市", @"台中市", @"高雄市", @"台北市", @"台中市", @"高雄市", @"台北市", @"台中市", @"高雄市"]; NSArray *prices = @[@"300~1000", @"1000~5000", @"5000以上"]; NSArray *types = @[@"商務旅館", @"民宿", @"飯店", @"汽車旅館"]; items = @[cities, prices, types]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return items.count; } -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return [items[component] count]; } -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return items[component][row]; } -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component==0) { cityIndex = row; } else if (component ==1) { priceIndex = row; } else if (component ==2){ typeIndex = row; } NSLog(@"縣市:%@ 價格:%@ 類型:%@", items[0][cityIndex], items[1][priceIndex], items[2][typeIndex]); } @end
留言
張貼留言