2014-09-10 24 views

回答

1

試試這個...

UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)]; 
myPickerView.delegate = self; 
myPickerView.showsSelectionIndicator = YES; 
[self.view addSubview:myPickerView]; 

高達這個寫在你的viewDidLoad,現在實現委託方法

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component { 
    // Handle the selection 
} 

// tell the picker how many rows are available for a given component 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    NSUInteger numRows = 5; 

    return numRows; 
} 

// tell the picker how many components it will have 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
return 1; 
} 

// tell the picker the title for a given component 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    NSString *title; 
    title = [@"" stringByAppendingFormat:@"%d",row]; 

    return title; 
} 

// tell the picker the width of each row for a given component 
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 
int sectionWidth = 300; 

return sectionWidth; 
}