2014-11-08 22 views
18

Tôi đang cố gắng tạo hộp văn bản khi được chọn UIPickerView mở ra với các lựa chọn để chọn. Sau khi được chọn, ẩn UIPickerView và mục đã chọn được hiển thị trong hộp văn bản. Tôi đã thử các đoạn mã khác nhau mà tôi tìm thấy trực tuyến nhưng tôi không thể làm cho nó hoạt động được. Nếu ai đó có thể đề xuất một mã hoàn chỉnh cho điều này hoặc cho tôi biết những gì tôi đang làm sai trong mã của tôi, điều đó sẽ cực kỳ tuyệt vời. Cám ơn rất nhiều.Hiển thị trường văn bản UIPickerView được chọn, sau đó ẩn sau khi đã chọn

Đây là mã của tôi:

@IBOutlet var textfieldBizCat: UITextField! 
@IBOutlet var pickerBizCat: UIPickerView! = UIPickerView() 

var bizCat = ["Cat One", "Cat Two", "Cat Three"] 


override func viewDidLoad() { 
    super.viewDidLoad() 

    var bizCatCount = bizCat.count 

    self.textfieldBizCat.inputView = pickerView 

} 

// returns the number of 'columns' to display. 
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{ 
    return 1 
} 

// returns the # of rows in each component.. 
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{ 
    return bizCat.count 
} 

func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! { 
    return bizCat[row] 
} 

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) 
{ 
    textfieldBizCat.text = "\(bizCat[row])" 

} 

Trả lời

39

Nếu tôi hiểu rõ câu hỏi của bạn, bạn muốn:

  1. Có một UITextField mà hiển thị một văn bản đã chọn
  2. Mở một bảng chọn khi nhấp chuột sử dụng trên UITextField
  3. Đóng bộ chọn khi một mục (trong bộ chọn) được chọn và đặt nó trong `UITextField

Đây là mã hoàn chỉnh để quản lý nó, bạn chỉ cần phải liên kết các đại biểu của UITextField của bạn:

@IBOutlet var textfieldBizCat: UITextField! 
@IBOutlet var pickerBizCat: UIPickerView! = UIPickerView() 

var bizCat = ["Cat One", "Cat Two", "Cat Three"] 


override func viewDidLoad() { 
    super.viewDidLoad() 
    pickerBizCat.hidden = true; 
    textfieldBizCat.text = bizCat[0] 
} 

// returns the number of 'columns' to display. 
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{ 
    return 1 
} 

// returns the # of rows in each component.. 
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{ 
    return bizCat.count 
} 

func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! { 
    return bizCat[row] 
} 

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) 
{ 
    textfieldBizCat.text = bizCat[row] 
    pickerBizCat.hidden = true; 
} 

func textFieldShouldBeginEditing(textField: UITextField) -> Bool { 
    pickerBizCat.hidden = false 
    return false 
} 

gì tôi đã thay đổi từ mã của bạn:

  • Dùng UITextFieldDelegate để hiển thị bộ chọn khi số UITextField được chọn
  • Ẩn bộ chọn khi mục được chọn và thiết lập UITextField
  • Đặt hàng đầu tiên của bảng chọn của bạn trong UITextField khi bất kỳ mục được chọn
+0

Cảm ơn sự giúp đỡ của bạn Ckouta. Nó bây giờ hiển thị "Cat One" trong trường văn bản, nhưng khi tôi nhấp vào nó, UIPicker sẽ không xuất hiện. – TimberWebDesign

+1

Bạn đã thiết lập đại biểu UITextField từ xib của bạn? Bạn có thể làm điều đó cũng theo chương trình, chỉ cần thêm 'textfieldBizCat.delegate = self'in' viewDidLoad' – tbaranes

+0

Oh, ok quên làm điều đó. Cũng cần phải thêm pickerBizCat.delegate = self to viewDidLoad. Bây giờ nó hoạt động! Cám ơn rất nhiều. – TimberWebDesign

0
// pressing the button again would hide the uipickerview. when pressed the first time, update the button's label to "done" , "hide" or whatever suits u! 
    @IBAction func propertyTypeButtonPressed(sender: UIButton)/* the name of your button's action*/ 
    { 
     count++; //declare it first 
     ViewContainigPickerView.hidden = false 
     self.view.endEditing(true) 

     if (count == 2) 
     { 
      ViewContainingPickerView.hidden = true /* if you placed your picker on a separate view for simplicity*/ 
      count = 0; 

     } 

    } 
0

Làm thế nào về phương pháp trong didSelectRow của bạn, bạn resignFirstResponder?

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) 
{ 
textfieldBizCat.text = bizCat[row] 
pickerBizCat.resignFirstResponder() 
} 
Các vấn đề liên quan