0

我想在我的视图加载后立即在 iPad 上显示一个弹出窗口,并将右上角的按钮作为源。弹出窗口在按钮点击时正确显示,但是当页面首次加载时,我无法找到一种在没有按钮点击的情况下显示它的方法。这可能吗?

这就是我所拥有的:

func displayOptions(sourceButton: UIBarButtonItem)
    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    if let popoverController = actionSheet.popoverPresentationController {
        var rect = CGRect(x: 0, y: 0, width: 0, height: 0)              
        popoverController.barButtonItem = sourceButton              
        popoverController.sourceRect = rect 
        popoverController.permittedArrowDirections = .up
    }
    viewController.present(actionSheet, animated: true, completion: nil)
}

此代码在按钮点击时正常工作,但如果我尝试从 viewDidLoad 或 viewDidAppear 调用此函数,应用程序会崩溃:

您必须通过警报控制器的 popoverPresentationController 提供此弹出窗口的位置信息。您必须提供 sourceView 和 sourceRect 或 barButtonItem。

4

2 回答 2

0
  1. 您需要提供sourceView.
  2. UIAlertController必须有 a title、 amessage或要显示的操作。

    // Create a bar button item in ViewDidLoad.
    let button1 = UIBarButtonItem(image: UIImage(named: "imagename"), style: .plain, target: self, action: Selector("action"))
    self.navigationItem.rightBarButtonItem  = button1
    
    
    // Call in ViewDidLoad
    displayOptions(sourceButton: button1)
    
    func displayOptions(sourceButton: UIBarButtonItem) {
        let actionSheet = UIAlertController(title: "title", message: "message", preferredStyle: .actionSheet)
        if let popoverController = actionSheet.popoverPresentationController {
          let rect = CGRect(x: 0, y: 0, width: 0, height: 0)
          popoverController.barButtonItem = sourceButton
          popoverController.sourceRect = rect
          popoverController.sourceView = self.view
          popoverController.permittedArrowDirections = .up
        }
        self.present(actionSheet, animated: true, completion: nil)
    }
    
于 2020-03-03T23:14:12.380 回答
0

尝试使用 UIPopoverPresentationControllerDelegate

代码示例:

  • viewDidLoad

    let buttonCustom = UIBarButtonItem(title: "Custom", style: .done, target: self, action: #selector(ViewController.customBarButtonAction))
    self.navigationItem.rightBarButtonItem  = buttonCustom
    displayOptions()
    
  • 显示选项

    func displayOptions() {
        let actionSheet = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
        if let popoverController = actionSheet.popoverPresentationController {
            let rect = CGRect(x: 0, y: 0, width: 0, height: 0)
            popoverController.sourceRect = rect
            popoverController.sourceView = self.view
            popoverController.permittedArrowDirections = .up
            popoverController.delegate = self
        }
        self.present(actionSheet, animated: true, completion: nil)
    }
    
  • 使用 UIPopoverPresentationControllerDelegate

    func prepareForPopoverPresentation(_ popoverPresentationController: UIPopoverPresentationController) {
        popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem
    } 
    
于 2020-03-04T07:10:19.240 回答