2015-12-15 94 views
0

我遇到了一些麻煩,我的ActionSheet。在iPhone上它工作正常,但在iPad上它只是崩潰。iPad上的ActionSheet崩潰(相機按鈕)

我有一個按鈕來顯示相機,但我無法在iPad上正確執行此操作。

代碼:

class pictureViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextViewDelegate { 
    @IBOutlet weak var picture: UIImageView! 
    override func viewDidLoad() { 
    } 

    @IBAction func choosePictureAction(sender: AnyObject) { 
     let alertPictureFrom = UIAlertController(title: "De dónde sacar la foto?", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet) 
     presentViewController(alertPictureFrom, animated: true, completion: nil) 
     alertPictureFrom.addAction(UIAlertAction(title: "Cámara", style: .Default, handler: {action in 
      let picker = UIImagePickerController() 
      picker.sourceType = .Camera 
      picker.delegate = self 
      picker.allowsEditing = true 
      self.presentViewController(picker, animated: true, completion: nil) 
     })) 

     alertPictureFrom.addAction(UIAlertAction(title: "Galería", style: .Default, handler: {action in 
      let picker = UIImagePickerController() 
      picker.sourceType = .PhotoLibrary 
      picker.delegate = self 
      picker.allowsEditing = true 
      self.presentViewController(picker, animated: true, completion: nil) 
     })) 
    } 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { 
     picture.image = image 
     picker.dismissViewControllerAnimated(true, completion: nil) 
    } 

} 

您的申請已經提交風格UIAlertControllerStyleActionSheet的UIAlertController()。具有此樣式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover。您必須通過警報控制器的popoverPresentationController爲此彈出窗口提供位置信息。您必須提供sourceView和sourceRect或barButtonItem。如果在呈現警報控制器時未知此信息,則可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供此信息。

+0

您必須提供此酥料餅的位置信息通過警報控制器的popoverPresentationController。 –

回答

2

由於錯誤消息說:「你必須提供一個sourceView和sourceRect或barButtonItem」通過popoverPresentationController

這將使UIKit的知道在哪裏呈現從酥料餅,然後你會得到可愛的行爲,彈出窗口出現在你指定的元素上。

例如:

alertPictureFrom.popoverPresentationController?.sourceRect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0) 
alertPictureFrom.popoverPresentationController?.sourceView = self.view 

現在,例如完全是用於演示目的,您將需要弄清楚的酥料餅的sourceViewsourceRect

參考文檔:

+0

偉大的工作!!!!! – FactorJose

+0

這對我來說是第一次,但是,如果ActionSheet再次出現,我再次收到錯誤。我在UIPopoverPresentationControllerDelegate方法prepareForPopoverPresentation中設置sourceView和sourceRect。 –

+0

哦,只是想出來。它的工作原理是如果我將sourceView和sourceRect設置爲不在委託方法中。不管怎麼說,多謝拉! –

0

嘗試(在一個UIButton的情況下):

picker.popoverPresentationController?.sourceView = testButton 
picker.popoverPresentationController?.sourceRect = testButton.bounds 
picker.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Right.bounds 
0

您的錯誤由於UIActionSheet

UIActionSheet不支持在ipad中。 爲iphone沒有在iphone UIActionsheet做另一種觀點認爲正常工作 爲iPad UIActionSheet開放像

UIActionSheetObjects.showFromRect(CGRectMake(150, 0, 300,200) ,inView:self.view, animated:YES) 

,它會爲你工作...