2019年5月22日 星期三

iOS swift 5: 關於 "UIAlertController actionSheet Exception"


解決方案來源:
1: 顯示警告訊息和選單的 UIAlertControlle
2: Swift 4 Tutorial : Show PopOver with TableView from Scratch (Source file link in 
description)
3: ActionSheet Popover on iPad in Swift

 *** Terminating app due to uncaught exception
 'NSGenericException', reason: 'Your application has presented a
  UIAlertController (<UIAlertController: 0x1160ef600>) of
   style UIAlertControllerStyleActionSheet from XXX.MainViewController (<XXX.MainViewController: 0x116036600>).
   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.'

意思就是沒有設置UIAlertController這個彈出窗口的位置信息



 @IBAction func BtnTestOnClick(_ sender: Any)
 {
    
  let alertController = UIAlertController(title: nil, message: "Alert message.", preferredStyle: .actionSheet)

  let defaultAction = UIAlertAction(title: "Default", style: .default, handler: { (alert: UIAlertAction!) -> Void in
    //  Do some action here.
  })

  let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { (alert: UIAlertAction!) -> Void in
    //  Do some destructive action here.
  })

  let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (alert: UIAlertAction!) -> Void in
    //  Do something here upon cancellation.
  })

  alertController.addAction(defaultAction)
  alertController.addAction(deleteAction)
  alertController.addAction(cancelAction)
  
  if let popoverController = alertController.popoverPresentationController {
   popoverController.sourceView = self.view
   popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
   popoverController.permittedArrowDirections = []
  }

  self.present(alertController, animated: true, completion: nil)
 }

沒有留言:

張貼留言