2014-11-04 13 views
6

Tôi đang cố gắng để thêm một pickerview vào ứng dụng iphone của tôi, nhưng thay vì hiển thị chuỗi từ mảng của tôi nó cho thấy dấu hỏi. Có ai biết tại sao không? Tôi đã cố gắng tìm nó ra cho giờ vừa qua ... Đây là mã của tôi về bộ điều khiển có chứa các pickerview:bộ chọnXem hiển thị dưới dạng dấu chấm hỏi thay vì dữ liệu?

class NewIssueViewController: UIViewController, UIPickerViewDelegate { 

    var componentArr = ["component1","component2","component3","component4"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 


    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func CancelPressed(sender: AnyObject) { 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 

    @IBAction func AddPressed(sender: AnyObject) { 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 

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

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

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

Trả lời

16

Bạn đặt nguồn dữ liệu quan điểm chọn của bạn, nhưng không thiết lập đại biểu của mình.

Kết quả là pickerView:titleForRow: không bao giờ được gọi.

+2

Đối với bất cứ ai tự hỏi làm thế nào để thiết lập các đại biểu, xem hình ảnh này:. Http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/543c1649e4b095b4c24a3d3c/1413224010678/dataSource-delegate. png? format = 750w – scottyseus

+0

hoặc thêm 'self.YourIBOutletForPicker.delegate = self' –

0

Bạn nên kết nối DatasourceDelegate trong bảng phân cảnh hoặc đặt chúng theo chương trình. Xem hình ảnh bên dưới.

enter image description here

0

New Swift 3 đã bị phản đối việc sử dụng các phương pháp trên,

tôi đã thực hiện những điều sau đây để làm cho họ làm việc. (Một số gạch dưới được thêm vào trong param

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

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

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

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
{ 
    /*if(row == 0) 
    { 
     self.view.backgroundColor = UIColor.whiteColor(); 
    } 
    else if(row == 1) 
    { 
     self.view.backgroundColor = UIColor.redColor(); 
    } 
    else if(row == 2) 
    { 
     self.view.backgroundColor = UIColor.greenColor(); 
    } 
    else 
    { 
     self.view.backgroundColor = UIColor.blueColor(); 
    }*/ 
} 
Các vấn đề liên quan