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

我似乎无法找到一个外部类管理ViewController中的视图的连接.我是iOS的新手,并且花了很多时间寻找解决方案.简单示例:

UIPickerView的子类

我创建了一个文件,它是UIPickerView的子类,并使其符合PickerView委托和数据源.

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

主视图控制器,带PletView的插座

在我的MainViewController中,我为我的选择器视图创建了一个插座.另外,在StoryBoard中,我将我的Picker View的“自定义类”连接到上面的MyPickerView.

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

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

我的问题:

>如何告诉我的MainViewController我的子类“MyPickerView”正在管理它的选择器视图?
>我如何启用子类和视图控制器之间的通信?

———————

更新:最终解决方案纳入@ Oscar的答案

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

现在,当调用PickerView“didSelectRow”时,我们需要通知我们的ViewController并传递所选的值.为了实现这一点,我使用了一个协议. (总结如下)这个主题花了我一些时间来学习,但是至关重要,所以我建议花时间与Protocols&代表团,如果这没有意义.

>在PickerView中创建一个带有func的协议,该func将用于与呈现此PickerView的ViewControllers对话:

protocol MyPickerViewProtocol {
    func myPickerDidSelectRow(selectedRowValue:Int?)
}

>在呈现PickerView的ViewController中,符合您的PickerView协议.通过这样做,您必须将func myPickerDidSelectRow放在ViewController中的某个位置:

class MyViewController: MyPickerViewProtocol {
    func myPickerDidSelectRow(selectedRowValue:Int?) { 
        //do stuff to update your ViewController 
    }
}

> @ Oscar的答案将把选择器视图连接到你的视图控制器,但还有最后一件事.为了让PickerView能够进行对话,您需要在PickerView中使用一个属性,它是对它所包含的视图控制器的引用.以下是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

>现在当我们的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]
    }
}

主视图控制器,带PletView的插座

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 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...