2012-01-30 73 views
0

我有一個表視圖&我必須在某些單元格中顯示文本字段。當用戶點擊文本字段時,我希望選擇器視圖以0到99之間的整數值顯示。一旦用戶在選擇器視圖中選擇了該項目,則需要在該特定文本字段上顯示相同的內容。任何幫助將真誠感謝。將UIPicker添加到文本框

+1

可以你告訴我你試過了什麼? – vishy 2012-01-30 13:13:25

回答

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

添加目標UR文本框功能

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

謝謝,代碼工作正常,但在那裏是一個輕微的問題,當我多次點擊文本字段,然後選擇器視圖不會在一個選擇上消失,而是在選擇了多次我已經單擊文本字段的輸入之後將會消失。 – cocoaNoob 2012-01-30 13:48:42

+0

當用戶選擇輸入時,我們還可以在標題上添加一個完成按鈕來關閉選取器視圖。 – cocoaNoob 2012-01-30 13:49:31

+0

好的,謝謝我設法解決了這兩個問題,謝謝你給我的方向:) – cocoaNoob 2012-01-30 14:32:02

0

創建一個選擇器視圖並在您的視圖控制器中實現委託和數據源方法。將選取器視圖設置爲文本字段的inputview。當用戶編輯文本字段時,它將彈出而不是鍵盤。

使用didSelect...委託方法可在用戶更改選取器時更新文本字段值。這條線的

+0

在這種情況下,選取器視圖出現在鍵盤的位置,但我想在我的文本字段下方 – cocoaNoob 2012-01-30 13:50:20

+0

您的意思是選取器視圖應始終可見? – jrturton 2012-01-30 14:08:19

+0

當我將選擇器視圖設置爲文本字段的輸入視圖時,我的選取器視圖出現在鍵盤的位置,但我希望我的選取器視圖顯示在我的文本字段後面 – cocoaNoob 2012-01-30 14:13:08

0

首先在viewDidLoad方法需要實例化PickerView,並把它添加到文本框

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

然後你必須實施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 
}