Удаление UIPickerView при нажатии кнопки быстро - PullRequest
1 голос
/ 24 января 2020

Как я могу закрыть представление uipickerview с помощью swift? Я создал UIPickerView в UITableView, так как хочу, чтобы он был заполнен элементами в UITableView. Тогда у меня просто UIButton появится на экране. Я хочу, чтобы при нажатии кнопки был удален UIPickerView.

//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(){
...