2012-10-25 56 views
0

我有一個應用程序,我有一個文本框,我從數據庫中顯示值。除了文本框外還有一個按鈕。我有選擇器,它包含4個值,即Custom,Walk,Run,Jog。如何在iPhone中將文本字段值設置爲選取器?

如果文本字段包含一個值,即如果包含'Walk',那麼點擊按鈕時,我將顯示其中填充了4個值的選擇器。但我的問題是,當按鈕被點擊時,應該在拾取器上設置值'Walk'。

這是我的代碼。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    currentarray = [[NSMutableArray alloc] initWithObjects:@"Custom",@"Walk",@"Run",@"Jog",nil]; 
     // Do any additional setup after loading the view from its nib. 
} 

這是點擊後打開撿拾我的按鈕操作:

-(IBAction)choose 
{ 
    actionsheet = [[UIActionSheet alloc]initWithTitle:[currentarray objectAtIndex:0] delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil]; 

    [actionsheet setTitle:@"Picker"]; 

    pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, 0, 0)]; 
    pickerView.dataSource=self; 
    pickerView.delegate=self; 
    pickerView.showsSelectionIndicator=YES; 
    [actionsheet addSubview:pickerView]; 
    [actionsheet showInView:self.view]; 
    [pickerView selectRow:[txtTextfield.text intValue ]-1 inComponent:0 animated:YES];   
    [actionsheet showInView:[[UIApplication sharedApplication]keyWindow]]; 
    [actionsheet setBounds:CGRectMake(0, 0, 320, 485)];   
} 

-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{  
    NSString *string; 
    string = [NSString stringWithFormat:@"%@",[currentarray objectAtIndex:row]]; 
    return string;  
} 

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 

-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{ 
    return [currentarray count]; 
} 

回答

相關問題