2015-01-22 18 views
59

Tôi đang sử dụng ActionSheet trong ứng dụng của mình. Trên iPhone của tôi nó hoạt động, nhưng nó không trên giả lập iPad.ActionSheet không hoạt động iPad

đây là mã của tôi:

@IBAction func dialog(sender: AnyObject) { 

    let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .ActionSheet) 
    let deleteAction = UIAlertAction(title: "Delete", style: .Default, handler: { 

     (alert: UIAlertAction!) -> Void in 
     println("Filtre Deleted") 
    }) 

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { 
     (alert: UIAlertAction!) -> Void in 
     println("Cancelled") 
    }) 

    optionMenu.addAction(deleteAction) 
    optionMenu.addAction(cancelAction) 

    self.presentViewController(optionMenu, animated: true, completion: nil) 
} 

Và lỗi của tôi:

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

+0

[Link] này (http://stackoverflow.com/questions/24224916/presenting-a-uialertcontroller-properly-on-an-ipad-using-ios-8) có thể giúp bạn. –

+4

ios 8 và ở trên không có hành động bảng UIActionController dụ u cần phải đặt loại như UIAlertControllerStyleActionSheet .... điều này có thể giúp bạn .... mặc dù uipopover được đề xuất cho iPad .... – Spynet

+0

Bạn phải trình bày nó như là một popover trên iPad – Totka

Trả lời

85

Bạn cần cung cấp một cái nhìn nguồn hoặc nút ngay trước khi trình bày optionMenu từ trên iPad của mình một UIPopoverPresentationController, Như nó nói trong lỗi của bạn. Điều này chỉ có nghĩa là trang tính hành động của bạn trỏ tới nút cho phép người dùng biết vị trí bắt đầu từ đó.

Ví dụ: nếu bạn đang trình bày tùy chọn của mình bằng cách nhấn vào mục trên thanh điều hướng bên phải. Bạn có thể làm một cái gì đó như thế này:

optionMenu.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem 

self.presentViewController(optionMenu, animated: true, completion: nil) 

hoặc bạn có thể thiết lập một cái nhìn như thế này: (bạn chỉ cần một trong những 2)

optionMenu.popoverPresentationController?.sourceView = yourView 

self.presentViewController(optionMenu, animated: true, completion: nil) 

Cũng nên nhớ rằng nếu bạn thay đổi UIAlertControllerStyle của bạn để Cảnh báo thay vì trang tính hành động, Bạn sẽ không cần chỉ định điều này. Tôi chắc chắn bạn phải tìm ra nhưng tôi chỉ muốn giúp đỡ bất cứ ai trên trang này.

16

Swift 3

Như đã nói trước đây, bạn nên cấu hình UIAlertController được trình bày trên một điểm cụ thể trên thiết bị iPad.

Ví dụ cho thanh điều hướng:

// 1 
    let optionMenu = UIAlertController(title: nil, message: "Choose an option", preferredStyle: .actionSheet) 

    // 2 
    let deleteAction = UIAlertAction(title: "Option 1", style: .default, handler: { 
     (alert: UIAlertAction!) -> Void in 
     print("option 1 pressed") 
    }) 
    let saveAction = UIAlertAction(title: "Option 2", style: .default, handler: { 
     (alert: UIAlertAction!) -> Void in 
     print("option 2 pressed") 
    }) 

    // 
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { 
     (alert: UIAlertAction!) -> Void in 
     print("Cancelled") 
    }) 


    // 4 

    optionMenu.addAction(deleteAction) 
    optionMenu.addAction(saveAction) 
    optionMenu.addAction(cancelAction) 

    // 5 

    optionMenu.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem 

    self.present(optionMenu, animated: true) { 
     print("option menu presented") 
    } 
4

thêm báo cáo trong các điều khoản sau đây trước khi trình bày.

tùy chọnMenu.popoverPresentationController.sourceView = self.view; tùy chọnMenu.popoverPresentationController.sourceRect = CGRectMake (0,0,1,0,1,0);

@IBAction func dialog(sender: AnyObject) { 
    ... 

    optionMenu.popoverPresentationController.sourceView = self.view; 
    optionMenu.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0); 

    self.presentViewController(optionMenu, animated: true, completion: nil) 
} 

nó sẽ hoạt động tốt.

+0

Làm việc hoàn hảo. Điều duy nhất là bạn cần phải thêm mục Thanh điều hướng bên trái, do đó menu popover không giống như nó sắp ra khỏi hư không –

14

Cùng một vấn đề với tôi. Tôi đã có một UIAlertController mà làm việc tốt trên điện thoại, nhưng bị rơi trên iPad. Tờ giấy bật lên khi một ô được nhấn từ một khung nhìn bảng.

Đối Swift 3, tôi đã thêm 3 dòng mã ngay trước khi trình bày nó:

 ... 

     sheet.popoverPresentationController?.sourceView = self.view 
     sheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection() 
     sheet.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) 


     self.present(sheet, animated: true, completion: nil) 
0

Chỉ cần một lưu ý rằng bạn cũng có thể nhận được lỗi này nếu bạn chưa liên kết các sourceview trong IB vào biến có liên quan trong ứng dụng của bạn.

Các vấn đề liên quan