簡體   English   中英

如何使用 UITextFields 在同一視圖 controller 中使用多個 UIPickerViews

[英]How to use Multiple UIPickerViews in same View controller using UITextFields

我試圖在 Objective-C 的一個視圖中擁有多個選擇器視圖。 到目前為止,我已經創建了 3 個不同的文本字段,如果我單擊它們,我希望其中三個具有不同的選擇器視圖。 所以讓我們說如果單擊文本字段#1,它將打開數組#1,文本字段#2 第二個和文本字段#第三個。 如果我單擊第一個和第二個文本字段相關的選擇器視圖顯示,但如果我單擊第三個選擇器視圖不顯示。

-(void)viewDidLoad{

isBloodGroupFieldSelected = YES;

isGenderGroupFieldSelected = YES;

// 加載視圖后做任何額外的設置。

bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-10,30)];

[self.view addSubview:bloodGroup];

txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 160, self.view.frame.size.width-10,30)];

txtField1.delegate = self;

[self.view addSubview:txtField1];

txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, 210, self.view.frame.size.width-10,30)];

txtField2.delegate = self;

[self.view addSubview:txtField2];

dataArray = [[NSMutableArray alloc]initWithObjects:@"A+",@"A-",@"B+",@"B-",@"O+",@"O-", nil];

genderArray = [[NSMutableArray alloc]initWithObjects:@"Male",@"Female", nil];

ageArray = [[NSMutableArray alloc]initWithObjects:@"Age1",@"Age2", nil];

myPickerView = [[UIPickerView alloc] init];

[myPickerView setDataSource: self];

[myPickerView setDelegate: self];

myPickerView.showsSelectionIndicator = 是;

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done:)];

UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-myPickerView.frame.size.height-50, 320, 50)];

[工具欄 setBarStyle:UIBarStyleBlackOpaque];

NSArray *toolbarItems = [NSArray arrayWithObjects:doneButton, nil];

[工具欄設置項目:工具欄項目];

bloodGroup.inputView = myPickerView;

bloodGroup.inputAccessoryView = 工具欄;

// txtField1

txtField1.inputView = myPickerView;

txtField1.inputAccessoryView = 工具欄;

txtField2.inputView = myPickerView;

txtField2.inputAccessoryView = 工具欄;

}

-(無效)完成:(id)發件人{

[血組辭職FirstResponder];

[txtField1 resignFirstResponder];

[txtField2 resignFirstResponder];

}

雜注標記 - UIPickerViewDataSource

// #3 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

if (isBloodGroupFieldSelected) {

返回 1;

}

否則如果(!isBloodGroupFieldSelected){

返回 1;

}

否則如果(!isGenderGroupFieldSelected){

返回 1;

}

返回0;

}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

if (isBloodGroupFieldSelected) {

返回[數據數組計數];

}

否則如果(!isBloodGroupFieldSelected)

{

返回[性別數組計數];

}

否則如果(!isGenderGroupFieldSelected)

{

返回[年齡數組計數];

}

返回0;

}

雜注標記 - UIPickerViewDelegate

// #5 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

if (isBloodGroupFieldSelected) {

返回數據數組[行];

}

否則如果(!isBloodGroupFieldSelected)

{

返回性別數組[行];

}

否則如果(!isGenderGroupFieldSelected)

{

返回年齡數組[行];

}

返回0;

}

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

if (isBloodGroupFieldSelected) {

bloodGroup.text = dataArray[行];

}

否則如果(!isBloodGroupFieldSelected)

{

txtField1.text = 性別數組[行];

}

否則如果(!isGenderGroupFieldSelected)

{

txtField2.text=ageArray[row];

}

}

  • (void)textFieldDidBeginEditing:(UITextField *)textField {

if (textField == bloodGroup) {

isBloodGroupFieldSelected = YES;

}

否則 if (textField == txtField1){

isBloodGroupFieldSelected = 否;

isGenderGroupFieldSelected = YES;

}

否則 if (textField == txtField2){

isGenderGroupFieldSelected = 否;

isBloodGroupFieldSelected = 否;

}

[myPickerView 重新加載所有組件];

}

這里的問題在於您在 titleForRow 方法中的代碼。 一旦將 false 分配給 isBloodGroupFieldSelected 方法,則指針總是移動到 else 部分,因為在移動到最后一個塊之前條件在那里匹配。

為此,您需要檢查這兩個條件

 (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

if (isBloodGroupFieldSelected) {

return dataArray[row];

}

else if(!isBloodGroupFieldSelected) && isGenderGroupFieldSelected

{

return genderArray[row];

}

else if(!isGenderGroupFieldSelected)

{

return ageArray[row];

}

return 0;

}

我建議您為此使用 textField 標記。 這對你來說會更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM