2

How can I dismiss a uipickerview view using swift? I have created the UIPickerView in UITableView as I want it to be populated with the elements in the UITableView. Then I have just a UIButton appear on the screen. I want it so that the UIPickerView will be removed when the button is pressed.

//being able to delete a row
// this method handles row deletion
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    // Edit Button
    let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, indexPath) in

        //saving the index of section
        self.contactIndex = indexPath.section

        //            print(self.tableViewData[indexPath.section].exerciseData[indexPath.row])
        print(self.tableViewData[indexPath.section])

        //setting exercisesInSelectedWorkout to exercise names within workout
        self.exercisesInSelectedWorkout = self.tableViewData[indexPath.section].exerciseData

        //creating uipicker
        var UIPicker: UIPickerView = UIPickerView()
        UIPicker.delegate = self as UIPickerViewDelegate
        UIPicker.dataSource = self as UIPickerViewDataSource
        let hello = UIPicker

        //Calling UIPicker done buttons:
        self.view.addSubview(self.TheDoneButton)
        self.TheDoneButton.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            self.TheDoneButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            self.TheDoneButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor ,constant: 40),
            ])

        //adding uipicker to screen
        self.view.addSubview(UIPicker)
        UIPicker.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            UIPicker.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            UIPicker.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -150),
            //UIPicker.widthAnchor.constraint(equalToConstant: self.view.frame.width - 64)
            ])

        //locking tableview
        tableView.alwaysBounceVertical = false
    })
    editAction.backgroundColor = UIColor.blue


    // Delete Action UITableView
    let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, indexPath) in

        //removing data from tableview
        self.tableViewData.remove(at: indexPath.section)
        tableView.deleteSections(IndexSet(integer: indexPath.section), with: .top)

        //Deleting Data from CoreData
        CoreDataManager.sharedInstance.deleteDataFromCoreData(contact: self.contacts[indexPath.section])
        print("Items removed from Table View")
        print("Row Deleted")
    })
    deleteAction.backgroundColor = UIColor.red
    return [editAction, deleteAction]
}


//UIPICKER Done button function (what happens when done is pressed)
@objc func uipickerDoneButtonPressed(){
kitchen800
  • 197
  • 1
  • 12
  • 36
  • Why you create UIPickerview programmatically? just create in pickerview in current viewcontroller storyboard and hide and show as per your conditions. @pete800 – kishor soneji Jan 24 '20 at 04:12
  • 2
    You want to call `resignFirstResponder()` or `self.view.endEditing(true)` – chirag90 Jan 24 '20 at 08:36

0 Answers0