2017-01-27 5 views
0

ユーザーがフィールドを選択したときに私のUIPickerviewが閉じないようにしようとしています。 UIPickerviewが自動的に解除されることなく、ユーザーがフィールドを選択できるようにしたい。私は、このような次のことをしようとしてたくさんのことを試してみましたが、それらのどれも助けなかっ:ユーザーがフィールドを選択したときにUIPickerViewが終了しないようにするにはどうすればよいですか?

_txtfield.hidden=NO; 
[_pickerView endEditing:NO]; 
[pickerView endEditing:NO]; 
[_txtfield endEditing:NO]; 
[self endEditing:NO]; 
_pickerView.hidden=NO; 

を-----------------ここではより多くのコードです----- -----------

@implementation FieldWithPickerView { 
    void(^pickerCallback)(NSInteger row); 
    CGRect myFrame; 


} 

-(void)viewDidAppear{ 

} 

-(void)commonInit:(CGRect)frame{ 



    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil]; 


    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:@{@"view":self.InputViewPicker}]]; 
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:@{@"view":self.InputViewPicker}]]; 
    [self.layer setCornerRadius:10.0f]; 
    [self setClipsToBounds:YES]; 


    _pickerView = [[UIPickerView alloc] init]; 
    _pickerView.delegate=self; 




    UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    [toolBar setTintColor:[UIColor grayColor]]; 
    UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(removePicker)]; 
    UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    [toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]]; 
    [self.txtfield setInputAccessoryView:toolBar]; 



    self.txtfield.inputView = _pickerView; 
    [_txtfield.inputView setBackgroundColor:[UIColor clearColor]]; 
    [_txtfield setBackgroundColor:[UIColor clearColor]]; 
    [email protected]"--Select---"; 


    _label_view.textColor=[UIColor whiteColor]; 

    _label_view.backgroundColor=SECURUSBLUE; 
    _label_view.textAlignment = NSTextAlignmentCenter; 

    _txtfield.textAlignment = NSTextAlignmentCenter; 




} 
-(void)removePicker 
{ 
    [_txtfield resignFirstResponder]; 
} 

-(void)pickerListenner:(void(^)(NSInteger row))handler 
{ 
    pickerCallback=handler; 

} 

- (id)initWithFrame:(CGRect)frame 
{ 
// NSLog(@"initwithframe picker"); 

    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     [self commonInit:frame]; 

    } 
    return self; 
} 

#pragma mark - Picker View Data source 

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

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

- (NSString *)pickerView:(UIPickerView *)pickerView 
      titleForRow:(NSInteger)row 
      forComponent:(NSInteger)component 
{ 
    if(_pickerData.count == 0) 
     return @"There is nothing"; 
    return [_pickerData objectAtIndex:row]; 
} 
#pragma mark - 

#pragma mark - UIPickerViewDelegate 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    // Code logic 
    NSLog(@"selected row --->%ld!!!!",(long)row); 
    pickerCallback(row); 
// _txtfield.text=[_pickerData objectAtIndex:row]; 

    _txtfield.hidden=NO; 


} 

- (NSInteger)selectedRowInComponent:(NSInteger)component 
{ 
    return component; 
} 
+0

テキストフィールドがフォーカスされている場合は、キーボードを表示する必要があるので、私はあなたが傾けると思います。 –

+0

あなたのピッカーをどのように表示し、どのようにそれを却下するかコードを投稿できますか? – Vladimir

+0

@Vladimirちょうど上記のコードを掲載しました。ありがとう。 –

答えて

0

あなたはUIPickerViewのデリゲートメソッドを実装するので、あなたのViewController<UIPickerViewDelegate>を追加する必要があります。あなたのviewDidLoad方法で

はそうのようなプロトコルに準拠:

pickerView.delegate = self; 

その後の行が選択されているときを検出するには、次のメソッドを追加します。

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    // do nothing because it will not dismiss 
} 

あなたはnumberOfRowsInComponentを実装する必要があります、numberOfComponents、およびtitleForRowデリゲートメソッドも実装されていますが、一度行が選択されると、ピッカービューは閉じられません。代理人メソッドdidSelectRowでピッカービューを手動で解除する必要があります。

+0

私はちょうど上記のコードを掲載しました。私はあなたが示唆していることをしましたが、フィールドを選択するとすぐに私のpickerviewが却下されます。ありがとう。 –

+0

あなたはここで何か異なったことをしています。何が 'UIMenuController'ですか? –

+0

機能には影響しません。 –

関連する問題