2012-01-30 8 views
0

私はテーブルビューを持っています&いくつかのセルにテキストフィールドを表示する必要があります。ユーザーがテキストフィールドをクリックすると、0から99までの整数値でピッカービューが表示されます。ユーザーがピッカービューで項目を選択すると、その特定のテキストフィールドに同じものを表示する必要があります。どんな助力も心から感謝します。テキストフィールドにUIPickerを追加する

+1

缶あなたは何を試してみましたか? – vishy

答えて

1
-(void) viewDidLoad 
{ 
arrayNo = [[NSMutableArray alloc] init]; 
[arrayNo addObject:@"1"]; 
[arrayNo addObject:@"2"]; 
[arrayNo addObject:@"3"]; 
// and so on.... 
    } 

ウルテキストフィールド機能を使用すると、PickerViewをインスタンス化し、その後、テキストフィールドにあなたを

UIPickerView *pickerView =[[UIPickerView alloc]init]; 
pickerView.delegate=self; 
pickerView.dataSource=self; 
pickerView.showsSelectionIndicator=YES; 
myTextField.inputView=pickerView; 

を追加する必要がありviewDidLoadメソッドで

UITextfield *entered2; 
    //..... some codes 
    [entered2 addTarget:self action:@selector(showPicker)forControlEvents:UIControlEventEditingDidBegin]; 
[myview addSubview:entered2]; 




    -(void) showPicker 
    { 
[entered2 resignFirstResponder]; 
    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,240,220,0)]; 

pickerView.delegate = self; 
pickerView.dataSource = self; 
pickerView.showsSelectionIndicator = YES;  

[self.view addSubview:pickerView]; 
[pickerView release]; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
    { 
    return [arrayNo count]; 
    } 
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
return 1; 
    } 
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
    { 
entered2.text=[arrayNo objectAtIndex:row]; 
[pickerView removeFromSuperview]; 
    } 
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
    { 
return [arrayNo objectAtIndex:row]; 
[pickerView removeFromSuperview]; 
    } 
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 
int sectionWidth = 300; 
return sectionWidth; 
    } 
+0

ありがとう、私はテキストフィールドを複数回クリックすると、ピッカービューは1つの選択を解除しないで、むしろそれはテキストフィールドをクリックしたその何度も入力を選択した後に消えてしまいます。 – cocoaNoob

+0

さらに、ユーザーが入力を選択したときに、ピッカービューを閉じるためにタイトルに完了ボタンが表示されます。 – cocoaNoob

+0

私はあなたに私に与える方向のおかげで、両方の問題を解決するために、大丈夫、感謝しました:) – cocoaNoob

0

ピッカービューを作成し、ビューコントローラでデリゲートメソッドとデータソースメソッドを実装します。ピッカービューをテキストフィールドのinputviewとして設定します。ユーザーがテキストフィールドを編集すると、キーボードの代わりにポップアップが表示されます。

ユーザーがピッカーを変更したときにテキストフィールド値を更新するには、didSelect...代理人メソッドを使用します。

+0

その場合、ピッカービューはキーボードの位置にありますが、テキストフィールドのすぐ下にあります – cocoaNoob

+0

ピッカービューは常に表示されるはずですか? – jrturton

+0

ピッカービューをテキストフィールドの入力ビューとして設定すると、ピッカービューはキーボードの位置に表示されますが、ピッカービューがテキストフィールドのすぐ後ろに表示されるようにします – cocoaNoob

0

まず、この行のターゲットを追加UIPickerViewデリゲートを実装する必要があります

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

    myTextField.text=[arrayWithData objectAtIndex:row]; 
} 

ここで、arrayWithDataは、PickerViewのソースを持つ配列です。

1

スウィフトIOSの:(注:要件によってVARS名前を変更してください)

ステップ1:宣言VARSと

var objPickerView : UIPickerView = UIPickerView() 

// select the picker view data values 
objPickerView.delegate=self; 
objPickerView.dataSource=self; 
objPickerView.showsSelectionIndicator=true; 

// show picker view when text field clicked 
self.smokingStatusText!.inputView = objPickerView 


// reload the component of picker 
self.objPickerView.reloadAllComponents() 

// select the value of picker status(Which row must be selected) 
self.objPickerView.selectRow(sStatus!, inComponent: PickerComponent.size.rawValue, animated: false) 

ステップ2オブジェクト:宣言デリゲートとプロトコル:

// programming mark ----- ---- ----- ----- ---- ----- ----- ---- ----- 

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
    return 1 
} 

// programming mark ----- ---- ----- ----- ---- ----- ----- ---- ----- 

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    return pickerData.count 
} 

//MARK: UIPickerView Delegates 

// programming mark ----- ---- ----- ----- ---- ----- ----- ---- ----- 

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

    smokingStatusText!.text = pickerData[row] 
    self.smokerStatusNum = String(row) 

} 

// programming mark ----- ---- ----- ----- ---- ----- ----- ---- ----- 

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 

    let titleData = pickerData[row] 

    let font:UIFont? = UIFont(name: "Georgia", size: 14.0) 

    let attrString = NSAttributedString(
     string: titleData, 
     attributes: NSDictionary(
      object: font!, 
      forKey: NSFontAttributeName)) 

    return attrString 
} 

// programming mark ----- ---- ----- ----- ---- ----- ----- ---- ----- 

//size the components of the UIPickerView 
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { 
    return 36.0 
} 

func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { 
    return 300 
} 
関連する問題