2013-04-08 95 views
0

我想知道我是如何創建一個選擇器視圖小時,然後我選擇出現在標籤....然後把這個小時在一個條件(中頻)。PickerView小時和顯示在標籤

任何人都可以幫助我嗎?

+0

你可以請詳細說明你已經嘗試了什麼,以及你可能收到的任何錯誤。 Thx – Mightymuke 2013-04-08 02:01:24

+0

只有使用[tag:xcode]標籤才能瞭解關於IDE本身的問題。謝謝! – Undo 2013-04-08 02:09:09

回答

0

試試這個: 在.h文件中定義這些變量:

UIPickerView  *picker; 
NSArray   *HOURS_24; 
UILabel   *hourLabel; 

在viewDidLoad中:

HOURS_24=[[NSArray alloc] initWithObjects: @" 8:00", @" 9:00", @" 10:00", @" 11:00", 
      @" 12:00", @" 13:00", @" 14:00", @" 15:00", @" 16:00", @" 17:00", @" 18:00", nil]; 

//Add picker in XIB or add it using code like below. 
picker=[[UIPickerView alloc]initWithFrame:CGRectMake:(100,100,100,100)]; 
picker.delegate=self; 
[self.view addSubview:picker]; 

hourLabel=[[UILable alloc]initWithFrame:CGRectMake: (20,100,50,20)]; 
[self.view addSubview:hourLabel]; 
[email protected]"This is hour label"; 

添加這些功能

#pragma mark PickerView DataSource 

- (NSInteger)numberOfComponentsInPickerView: 
    (UIPickerView *)pickerView 
{ 
    return 1; 
} 
- (NSInteger)pickerView:(UIPickerView *)pickerView 
    numberOfRowsInComponent:(NSInteger)component 
{ 
    return [HOURS_24 count]; 
} 
- (NSString *)pickerView:(UIPickerView *)pickerView 
    titleForRow:(NSInteger)row 
    forComponent:(NSInteger)component 
{ 
    return [HOURS_24 objectAtIndex:row]; 
} 

實施委託:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row 
    inComponent:(NSInteger)component 
{ 
    hourLabel.text=[HOURS_24 objectAtIndex:row]; 
}