2016-10-26 72 views
0

我在我的應用程序中有一個文本框,用戶可以在其中選擇他們所屬的組以及其他幾個字段。當他們到達組TextField時,它彈出一個UIPickerView,它有幾個已經創建的選項,以及一個「創建新組」的選項。當選擇該選項時,我希望UIPickerView消失,並讓鍵盤彈出,以便它們可以再次輸入。我可以讓選取器視圖出現並消失,但無法取回鍵盤。這是迄今爲止的代碼。iOS用UIPickerView替換鍵盤然後回到鍵盤

-(void)addPickerView{ 

    __block NSMutableArray *arr = [[NSMutableArray alloc] init]; 

    PFQuery *rejectedNumber = [PFQuery queryWithClassName:@"Group"]; 
    [rejectedNumber orderByAscending:@"GroupName"]; 
    [rejectedNumber findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 



     if (!objects) { 
      // Did not find any UserStats for the current user 

      NSLog(@"NotFound"); 
     } else { 
      pickerArray = objects; 
      [myPickerView reloadAllComponents]; 

     } 
    }]; 

    //pickerArray = GoGo; 
    self.theView.signUpView.additionalField.delegate = self; 
    [self.theView.signUpView.additionalField setPlaceholder:@"Choose Group"]; 
    myPickerView = [[UIPickerView alloc]init]; 
    myPickerView.dataSource = self; 
    myPickerView.delegate = self; 
    myPickerView.showsSelectionIndicator = YES; 
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
            initWithTitle:@"Done" style:UIBarButtonItemStyleDone 
            target:self action:@selector(finishIt)]; 
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame: 
          CGRectMake(0, self.view.frame.size.height- 
            self.theView.signUpView.additionalField.frame.size.height-50, 320, 50)]; 
    [toolBar setBarStyle:UIBarStyleBlackOpaque]; 
    NSArray *toolbarItems = [NSArray arrayWithObjects: 
          doneButton, nil]; 
    [toolBar setItems:toolbarItems]; 
    self.theView.signUpView.additionalField.inputView = myPickerView; 
    self.theView.signUpView.additionalField.inputAccessoryView = toolBar; 

} 
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return 1; 
} 
-(NSInteger)pickerView:(UIPickerView *)pickerView 
numberOfRowsInComponent:(NSInteger)component{ 
    return [pickerArray count] + 1; 
} 

#pragma mark- Picker View Delegate 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow: 
(NSInteger)row inComponent:(NSInteger)component{ 
    if (row == 0) 
     [self.theView.signUpView.additionalField.inputView removeFromSuperview]; 
     [self.theView.signUpView.additionalField becomeFirstResponder]; 

    } 
    else { 

    [self.theView.signUpView.additionalField setText:self.theGroup]; 
    } 
} 
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    NSString *groupName = @"Create New Group"; 
    if (row != 0) 
    { 
     PFObject *object = pickerArray[row - 1]; // -1 to handle the array index 
     self.theGroup = object[@"GroupName"]; 
     groupName  = object[@"GroupName"]; 
    } 
    return groupName; 
} 

回答

1

i是注意到的第一件事情,你錯過了一個大括號這裏如果以後條件:

if (row == 0) 
    [self.theView.signUpView.additionalField.inputView removeFromSuperview]; 
    [self.theView.signUpView.additionalField becomeFirstResponder]; 

} 

而是使用此:

if (row == 0) { 
    [self.theView.signUpView.additionalField resignFirstResponder]; 
    self.theView.signUpView.additionalField.inputView = nil; 
    [self.theView.signUpView.additionalField becomeFirstResponder]; 
} 
+0

謝謝,這工作。 {在我的代碼中,我剛剛發佈時就意外刪除了它。我怎樣才能改變它,使PickerView開始突出顯示在一個空行上?現在第一行是「創建新組」,您必須離開然後再選擇它。思考? – user717452