popoverPresentationController return nil macOS 14.0 (Catalyst) App

70 Views Asked by At

Before macOS 13, it worked fine, but when I presented the alert controller with a popover presentation controller, it returned nil. if anyone has an idea then please tell me.

@IBAction func action(_ sender: UIButton) {
    // Create an instance of UIAlertController with the preferred style as .actionSheet
    let alertController = UIAlertController(title: "Action Sheet", message: "Choose an action:", preferredStyle: .actionSheet)
    
    // Create action buttons for the action sheet
    let option1Action = UIAlertAction(title: "Option 1", style: .default) { _ in
        // Handle Option 1 selection
        print("Option 1 selected")
    }
    
    let option2Action = UIAlertAction(title: "Option 2", style: .default) { _ in
        // Handle Option 2 selection
        print("Option 2 selected")
    }
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in
        // Handle Cancel selection or do nothing
        print("Action sheet canceled")
    }
    
    // Add the action buttons to the action sheet
    alertController.addAction(option1Action)
    alertController.addAction(option2Action)
    alertController.addAction(cancelAction)
    alertController.modalPresentationStyle = .popover
    // Present the action sheet
    if let popoverController = alertController.popoverPresentationController {
        // Configure the popover for iPad (optional)
        popoverController.sourceView = sender
        popoverController.sourceRect = sender.bounds
        popoverController.permittedArrowDirections = .up
    }
    
    present(alertController, animated: true, completion: nil)
}

This condition is always false.

if let popoverController = alertController.popoverPresentationController {

0

There are 0 best solutions below