ios - 从外部类管理 UIPickerView - 使用 Swift

标签 ios swift delegates subclass uipickerview

我似乎找不到让外部类在 ViewController 中管理 View 的连接。我是 iOS 的新手,花了很多时间寻找解决方案。简单示例:

UIPickerView 的子类

我创建了一个文件,该文件是 UIPickerView 的子类,并使其符合 PickerView 委托(delegate)和数据源。

class MyPickerView: UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource {
    //In here I conform to all the required methods...no problems there
}

带有 PickerView 导出的主视图 Controller

在我的 MainViewController 中,我为我的选择器 View 创建了一个 socket 。此外,在 StoryBoard 中,我将我的 Picker View 的“自定义类”连接到上面的 MyPickerView。

class MainViewController: UIViewController {
    @IBOutlet weak var myPickerView: UIPickerView!

    override func viewDidLoad() {
        //how do I hookup my picker view class
    }
}

我的问题:

  1. 如何告诉我的 MainViewController 我的子类“MyPickerView”正在为其管理选择器 View ?

  2. 如何启用子类和 View Controller 之间的通信?

--------------------

更新:结合@Oscar 的回答的最终解决方案

@Oscar 下面的建议很棒。澄清一下,我希望我的 PickerView 子类成为 UIPickerView Delegate,因为 Picker 将始终具有相同的 UI,并且 UI 有许多 PickerView 委托(delegate)方法。 (attributedTitleForRow、widthForComponent、rowHeightForComponent 等)我不想在每个使用此 PickerView 的 ViewController 中调用这些委托(delegate)方法。

现在,当 PickerView“didSelectRow”被调用时,我们需要通知我们的 ViewController 并传递所选择的值。为了让它工作,我使用了一个协议(protocol)。 (总结如下)这个主题花了我一段时间来学习,但它很重要,所以如果这没有意义,我建议花时间学习协议(protocol)和授权。

  1. 在 PickerView 中创建一个带有函数的协议(protocol),该函数将用于与呈现此 PickerView 的 ViewController 对话:

    protocol MyPickerViewProtocol {
        func myPickerDidSelectRow(selectedRowValue:Int?)
    }
    
  2. 在呈现 PickerView 的 ViewController 中,遵循您的 PickerView 协议(protocol)。通过这样做,您必须将 func myPickerDidSelectRow 放置在您的 ViewController 中的某个位置:

    class MyViewController: MyPickerViewProtocol {
        func myPickerDidSelectRow(selectedRowValue:Int?) { 
            //do stuff to update your ViewController 
        }
    }
    
  3. @Oscar 在下面的回答会将选择器 View 连接到您的 View Controller ,但还有最后一件事。为了让 PickerView 回应,您需要在 PickerView 中有一个属性,这是对它所包含的 View Controller 的引用。下面是 PickeView 和 ViewController 类的透视图:

    //PickerView Subclass ------------------
    protocol MyPickerViewProtocol {
        func myPickerDidSelectRow(selectedRowValue:Int?)
    }
    
    class MyPickerView: UIPickerView {
        //Note: this var is of type your Picker protocol above. Because the ViewController will conform to the protocol, this var will be the reference (or the pointer) to the protocol func you implement in your ViewController...which is myPickerDidSelectRow
        var propertyThatReferencesThisViewController:MyPickerViewProtocol?
    }        
    
    //ViewController Class ----------------
    myPicker = MyPickerView()
    myPickerView.dataSource = myPicker //note: myPickerView is the outlet of type UIPickerView in your ViewController
    myPickerView.delegate = myPicker
    //HERE'S THE PROPERTY from our PickerView subclass that will point to this ViewController's protocol methods that we implemented. From the MyPickerViewProtocol
    myPicker.propertyThatReferencesThisViewController = self
    
  4. 现在,当在我们的 PickerView 中选择了一行时,让我们使用我们的属性告诉 ViewController:propertyThatReferencesThisViewController

    class MyPickerView: UIPickerView {
        //This Property points to the ViewController conforming to the protocol. This property will only be able to access the stuff you put in the protocol. It won't access everything in your ViewController
        var propertyThatReferencesThisViewController:MyPickerViewProtocol?
    
        //didSelectRow UIPickerView Delegate method that apple gives us
        func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            //get your picker values that you need
            let theRowValue = someArray[row]
            propertyThatReferencesThisViewController?.myPickerDidSelectRow(theRowValue)
    
            //the ViewController func will be called passing the row value along
        }
    }
    

最佳答案

子类 Pickerview

class MyPickerView: UIPickerView, UIPickerViewDataSource, UIPickerViewDelegate {

    var oficinas = ["oficina 1", "Oficinas 2", "Oficina 3"]

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

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

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

带有 PickerView 导出的主视图 Controller

class MainViewController: UIViewController {
    @IBOutlet weak var myPickerView: UIPickerView!

    var pickerOficinas: MyPickerView!

    override func viewDidLoad() {
        //how do I hookup my picker view class
        pickerOficinas = MyPickerView()
        myPickerView.delegate = pickerOficinas
        myPickerView.dataSource = pickerOficinas
    }
}

关于ios - 从外部类管理 UIPickerView - 使用 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32405373/

相关文章:

ios - 一个配置文件超过一个团队

ios - Swift:将数据从 tableView 显示到另一个 ViewController(JSON、Alamorife、AlamofireImage)

ios5 - xcode 4.4.1 - AddThis 配置

swift - 了解 SceneKit 的 SIMD

swift - Xcode快速在Firebase数据更新上进行内存填充

iphone - 当使用 [[UIApplication sharedApplication] delegate] 时

ios - 由于内部保留周期,ViewController 没有被释放

ios - 纹理又名 AsyncDisplayKit 支持 PHLivePhotoView 吗?

ios - 当您将 self 分配给委托(delegate)时,究竟会发生什么?

delegates - Kotlin 委托(delegate)更有趣