2015-01-22 1 views
59

내 응용 프로그램에서 ActionSheet를 사용하고 있습니다. 제 아이폰에서는 작동하지만 iPad 시뮬레이터에서는 그렇지 않습니다. 그것이 말하는대로, 그냥 아이 패드 자사의 UIPopoverPresentationController에 있기 때문에 optionMenu을 제시하기 전에 소스보기 또는 버튼을 제공해야작업 시트가 작동하지 않습니다. iPad

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] (http://stackoverflow.com/questions/24224916/presenting-a-uialertcontroller-properly-on-analipad-using-ios-8)이 도움이 될 수 있습니다. –

+4

ios 8 이상 동작 시트가 없습니다. UIActionController 인스턴스 UIAlertControllerStyleActionSheet ...로 형식을 설정해야합니다.이 작업은 도움이 될 수 있지만 .......... uipopover가 iPad 용으로 제안되었지만 .... – Spynet

+0

당신은 ipad에 popover – Totka

답변

85

:

@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) 
} 

그리고 내 오류 :

내 코드입니다 실수로 이것은 액션 시트가 버튼을 가리키고 있음을 알려주는 것입니다.

예를 들어 오른쪽 탐색 모음 항목을 탭하여 optionMenu를 표시하는 경우.

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

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

또는이 같은보기를 설정할 수 있습니다 : 당신은 이런 식으로 뭔가를 할 수 또한 당신이 당신의 UIAlertControllerStyle을 변경하면 것을 명심

optionMenu.popoverPresentationController?.sourceView = yourView 

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

(당신은이 둘 중 하나가 필요합니다) 액션 시트 대신 경고하라. 이것을 지정하지 않아도된다. 나는 당신이 그것을 알아 냈음에 틀림 없겠지만, 나는이 페이지를 가로막는 사람을 돕고 싶었다.

16

스위프트 3

으로는 아이 패드의 특정 지점에 표시 할 UIAlertController를 구성해야합니다 전에 말했다. 네비게이션 바

예 : 발표하기 전에

// 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

는 다음과 같은 측면에서 문을 추가합니다.

optionMenu.popoverPresentationController.sourceView = self.view; optionMenu.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) 
} 

+0

완벽하게 일했다. 유일한 것은 왼쪽 네비게이션 바 항목을 추가해야하기 때문에 popover 메뉴가 보이지 않는 것처럼 보입니다. –

14

같은 문제가 있습니다. 나는 전화로 잘 작동하는 UIAlertController를 가지고 있지만, iPad에서는 추락했습니다. 테이블 뷰에서 셀을 탭하면 시트가 나타납니다.

는 스위프트 3, 나는 바로 그것을 제시하기 전에 코드의 3 개 라인을 추가 : 당신은 관련 변수에 IB에서 sourceview를 연결하지 않은 경우 당신이 오류를 얻을 수 있습니다

 ... 

     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

그냥 메모 앱에서