Programing

ActionSheet가 작동하지 않는 iPad

crosscheck 2020. 10. 12. 07:15
반응형

ActionSheet가 작동하지 않는 iPad


내 응용 프로그램에서 ActionSheet을 사용하고 있습니다. 내 iPhone에서는 작동하지만 iPad 시뮬레이터에서는 작동하지 않습니다.

이것은 내 코드입니다.

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

그리고 내 오류 :

포착되지 않은 예외 'NSGenericException'으로 인해 앱 종료, 이유 : '애플리케이션이 스타일 UIAlertControllerStyleActionSheet의 UIAlertController ()를 제공했습니다. 이 스타일을 사용하는 UIAlertController의 modalPresentationStyle은 UIModalPresentationPopover입니다. 경고 컨트롤러의 popoverPresentationController를 통해이 팝 오버에 대한 위치 정보를 제공해야합니다. sourceView 및 sourceRect 또는 barButtonItem을 제공해야합니다. 경고 컨트롤러를 표시 할 때이 정보를 알 수없는 경우 UIPopoverPresentationControllerDelegate 메서드 -prepareForPopoverPresentation에 제공 할 수 있습니다. '


iPad에서는 UIPopoverPresentationController가 있기 때문에 optionMenu를 표시하기 직전에 소스보기 또는 버튼을 제공해야합니다. 이는 액션 시트가 버튼을 가리키고있어 사용자에게 시작 위치를 알려준다는 의미입니다.

예를 들어 오른쪽 탐색 모음 항목을 탭하여 optionMenu를 표시하는 경우. 다음과 같이 할 수 있습니다.

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

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

또는 다음과 같은보기를 설정할 수 있습니다. (이 두 가지 중 하나만 필요)

optionMenu.popoverPresentationController?.sourceView = yourView

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

또한 UIAlertControllerStyle을 작업 시트 대신 Alert로 변경하면이를 지정할 필요가 없습니다. 나는 당신이 그것을 알아 냈을 것이라고 확신하지만 나는이 페이지를 접하는 모든 사람들을 돕고 싶었습니다.


나에게도 같은 문제입니다. 전화에서는 잘 작동하지만 iPad에서는 충돌하는 UIAlertController가 있습니다. 테이블보기에서 셀을 탭하면 시트가 나타납니다.

Swift 3의 경우 발표 직전에 3 줄의 코드를 추가했습니다.

        ...

        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)

스위프트 3

앞서 말했듯이 iPAD의 특정 지점에 표시되도록 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")
    }

제시하기 전에 다음 용어로 진술을 추가하십시오.

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

잘 작동합니다.


화살표없이 중앙에 표시하려면 [ Swift 3+ ] :

if let popoverController = optionMenu.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(optionMenu, animated: true, completion: nil)

IB의 sourceview를 앱의 관련 변수에 연결하지 않은 경우에도이 오류가 발생할 수 있습니다.

참고 URL : https://stackoverflow.com/questions/28089898/actionsheet-not-working-ipad

반응형