如何从UI选择器视图重新加载数据并通过过渡关闭选择器视图

时间:2019-04-02 02:51:25

标签: ios swift

class MainVC: UIViewController {

    var datasourceForUsers :UserPicker!
    var userPicker :UIPickerView!
    var toolBar: UIToolbar!
    override func viewDidLoad() {

 super.viewDidLoad()

        userPicker = UIPickerView()
        toolBar = UIToolbar()
        datasourceForUsers = UserPicker()
 }

    @IBAction func onMoreTapped(){

        NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil)
    }

    @IBAction func showRegisteredUsers(_ sender: Any) {

        createUserPicker()
        configureToolBar(toolBar)
    }


    func createUserPicker() {

        userPicker.dataSource = datasourceForUsers
        userPicker.delegate = datasourceForUsers
        //Customizations
        userPicker.backgroundColor = .black
        userPicker.frame = CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 300)
        self.view.addSubview(userPicker)
    }

    func configureToolBar(_ toolBar:UIToolbar  ){

        toolBar.sizeToFit()

        toolBar.barTintColor = .black
        toolBar.tintColor = .white

        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(MainVC.dismissKeyboard))
        toolBar.setItems([doneButton], animated: false)
        toolBar.isUserInteractionEnabled = true
        toolBar.frame = CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 50)
        self.view.addSubview(toolBar)


    }

    override func viewWillAppear(_ animated: Bool) {
        userPicker.reloadAllComponents()
        toolBar.reloadInputViews()
    }


    @objc func cancelDatePicker(){
        self.view.endEditing(true)
        }

        @objc func dismissKeyboard() {
           self.resignFirstResponder()

        }
 }

我使用上面的代码在UIPickerView上显示了一些数据。它工作正常。但是当我更改存储数据的数据源(添加新数据)时,它不会反映在UI选择器上。这样做我必须再次运行该应用程序。第二件事是我无法通过过渡关闭UI选择器。

0 个答案:

没有答案