2011-08-15 37 views
0

Goodday,當它保持在底部獲取UIPickerView的價值的UITextField和旋轉

我有以下問題:

在我看來,我有一個texfield,對此我在代碼中創建。的UITextField越來越建立在這樣的代碼:

UITextField *filiaalSelection = [[UITextField alloc] init]; 
filiaalSelection.frame = CGRectMake(130, 100, 200, 30); 
filiaalSelection.borderStyle = UITextBorderStyleLine; 
filiaalSelection.delegate = self; 

現在,當我開始編輯文本框,一個UIPickerView需要彈出。我在下面的代碼中這樣做:

-(void)textFieldDidBeginEditing:(UITextField *)filiaalSelection { 
[filiaalSelection resignFirstResponder]; 

UIPickerView *myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, scrollView.frame.size.height - 225, scrollView.frame.size.width, 180)]; 

[myPicker setDelegate:self]; 
[myPicker setDataSource:self]; 
myPicker.showsSelectionIndicator = YES; 

[self.view addSubview:myPicker]; 


} 

現在我需要做2件事。首先是: 獲取我的UIPickerView的值並將其插入到UITextField中。下面的方法返回選擇的UIPickerView的當前值:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 

    [stringsArray objectAtIndex:row]; 

} 

但問題是,我不知道如何將值發送到文本框。而且我還需要在UIPickerView上面完成一個按鈕。這是最好的方法是什麼?

回答

1

你有什麼理由不能這麼做嗎?

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 

    [filiaalSelection setText:(NSString *)[stringsArray objectAtIndex:row]]; 
} 

至於完成按鈕,讓一切變得簡單了自己,你應該做的選擇器查看文本字段inputView。然後當字段開始或結束編輯時(當調用[textfield resignFirstResponder];時)它會自動顯示和解除。

然後,您可以將按鈕添加到視圖的某處,或者將其設置爲文本字段的inputAccessoryView,如果您希望它被附加到選取器視圖的頂部。

+0

我在另一種方法中製作UITextField:filiaalSelection。所以我不能把價值傳遞給那個..因爲它還沒有被製造出來。 –

+0

您需要將其設置爲可訪問,以便設置它。爲什麼不在頭文件中聲明它? – mjisrawi

+0

Thnx,我在Objective-C方面不是很有經驗。但是,這工作。 –