2014-06-09 17 views
7

Chỉ cần bắt đầu ở Swift và gặp khó khăn trong cách gọi phương pháp đại biểu cho một UIPickerViewĐại biểu Methods in Swift cho UIPickerView

Cho đến nay tôi đã thêm UIPickerViewDelegate đến lớp học của tôi như vậy:

class ExampleClass: UIViewController, UIPickerViewDelegate 

Tôi có cũng tạo UIPickerView của tôi và đặt đại biểu cho nó:

@IBOutlet var year: UIPickerView 
year.delegate = self 

Bây giờ tôi chỉ gặp sự cố khi chuyển mã sau thành mã Swift:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 

Bất kỳ trợ giúp sẽ được đánh giá

Trả lời

0

Các đại biểu không chịu trách nhiệm kêu gọi phương thức đó. Thay vào đó nó được gọi bởi nguồn dữ liệu của UIPickerView. Đây là hai chức năng gọi bằng các nguồn dữ liệu UIPickerView mà bạn cần phải thực hiện:

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

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

Để đảm bảo các chức năng này được gọi là, lớp học của bạn cũng nên thực hiện các giao thức nguồn dữ liệu:

class ExampleClass: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource 

Và bạn nguồn dữ liệu chọn cần được thiết lập:

year.dataSource = self 
11

Đó thực sự là một phương pháp trong giao thức UIPickerViewDataSource, vì vậy bạn sẽ muốn chắc chắn rằng bạn thiết lập view chọn của bạn của dataSource thuộc tính: year.dataSource = self. Cách Swift bản địa có vẻ là để thực hiện các giao thức trong phần mở rộng lớp, như thế này:

class ExampleClass: UIViewController { 
    // properties and methods, etc. 
} 

extension ExampleClass: UIPickerViewDataSource { 
    // two required methods 

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

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

extension ExampleClass: UIPickerViewDelegate { 
    // several optional methods: 

    // func pickerView(pickerView: UIPickerView!, widthForComponent component: Int) -> CGFloat 

    // func pickerView(pickerView: UIPickerView!, rowHeightForComponent component: Int) -> CGFloat 

    // func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! 

    // func pickerView(pickerView: UIPickerView!, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString! 

    // func pickerView(pickerView: UIPickerView!, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView! 

    // func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) 
} 
+1

Sử dụng một phần mở rộng để thực hiện một giao thức là tốt đẹp vì nó gói gọn tất cả các mã liên quan đến các giao thức ở một nơi, và nếu bạn thay đổi suy nghĩ của bạn về nó trong tương lai, bạn biết chính xác những gì bạn có thể loại bỏ :) – Jiaaro

+0

Chính xác! Và vì '# pragma' đã biến mất và phương thức' // MARK ... 'không hoạt động, đây là cách duy nhất để sắp xếp mọi thứ vào lúc này. ;) –

+0

Nate. Sử dụng cách này làm cách nào để đảm bảo rằng UIPickerView của tôi đang được sử dụng? – user3723426

Các vấn đề liên quan