ice-bit / DineUI

Building a UI app for a previous console project; the core functionality is restaurant management. Check the URL below 👇 for console app!
https://github.com/sek-r0/Dine
0 stars 0 forks source link

UIAlertControllerStyleActionSheet requires popoverPresentationController details on iPad #1

Closed ice-bit closed 4 months ago

ice-bit commented 5 months ago

Description

When presenting a UIAlertController of style UIAlertControllerStyleActionSheet from Dine.AddItemViewController, I receive the following error message:

Your application has presented a UIAlertController (<UIAlertController: 0x1040c4400>) of style UIAlertControllerStyleActionSheet from Dine.AddItemViewController (<Dine.AddItemViewController: 0x112816e00>). The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.

Steps to Reproduce

  1. Present a UIAlertController of style UIAlertControllerStyleActionSheet from any view controller.
  2. Attempt to present it without specifying the sourceView, sourceRect, or barButtonItem.

Expected Behavior

The action sheet should be presented without any error on all devices, particularly iPads, by providing the necessary popoverPresentationController properties.

Actual Behavior

The application throws an error and the action sheet does not appear correctly on iPads.

Code Example

Here is an example of the code causing the issue:

@objc private func selectMenuSectionButtonTapped(_ sender: UIButton) {
        print("menu selection button tapped")
        let alert = UIAlertController(title: "Select Section", message: "\n\n\n\n\n\n\n\n\n", preferredStyle: .actionSheet)
        alert.popoverPresentationController?.canOverlapSourceViewRect = true
        let pickerViewFrame = CGRect(x: 0, y: 50, width: alert.view.bounds.width - 20, height: 200)
        pickerView = UIPickerView(frame: pickerViewFrame)
        pickerView.delegate = self
        pickerView.dataSource = self

        alert.view.addSubview(pickerView)

        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        alert.addAction(UIAlertAction(title: "Done", style: .default, handler: { _ in
            let selectedIndex = self.pickerView.selectedRow(inComponent: 0)
            let selectedSection = self.pickerData[selectedIndex].rawValue
            self.selectedMenuSection = MenuSection(rawValue: selectedSection)
            self.sectionSelectionButton.setTitle(selectedSection, for: .normal)
        }))

        present(alert, animated: true, completion: nil)
    }

Environment

ice-bit commented 4 months ago

Issue is closed but the Picker View in not rendered inside the view!