2014-09-25 2 views
43

내 ActionSheet에 큰 문제가 있습니다. 아이폰에 그것은 잘 작동하지만, 아이 패드에 그것은 단지 내가 그것을 작동 아이폰에 말했듯이 Swift UIAlertController -> ActionSheet iPad iOS8이 충돌 함 현재

내가 하나 개의 버튼

import UIKit 

extension ViewController : UIActionSheetDelegate { 

    func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) { 

     if actionSheet.tag == 0 { 
      if buttonIndex == 1 { 
       // doing something for "product page" 
      } else if (buttonIndex == 2) { 
       // doing something for "video" 
      } 
     } 
    } 

} 

class ViewController: UIViewController, UIActionSheetDelegate { 
    @IBAction func test(sender: AnyObject) { 

     let systemVersion: NSInteger = (UIDevice.currentDevice().systemVersion as NSString).integerValue 
     if systemVersion < 8 { 
      // iOS7: 
      let action:UIActionSheet = UIActionSheet(title: "Change Map Type", delegate: self, cancelButtonTitle: "Back", destructiveButtonTitle: nil, otherButtonTitles: "Product Page", "Video") 
      action.tag = 0 
      action.showInView(self.view) 
     } else { 
      // iOS8: 
      let alertController: UIAlertController = UIAlertController(title: "Change Map Type", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet) 
      let cancelAction: UIAlertAction = UIAlertAction(title: "Back", style: UIAlertActionStyle.Cancel, handler: nil) 
      let button1action: UIAlertAction = UIAlertAction(title: "Product Page", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) ->() in 
       // doing something for "product page" 
      }) 
      let button2action: UIAlertAction = UIAlertAction(title: "Video", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) ->() in 
       // doing something for "video" 
      }) 
      alertController.addAction(cancelAction) 
      alertController.addAction(button1action) 
      alertController.addAction(button2action) 

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

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

} 

와 함께 새로운 프로젝트를 생성 충돌,하지만 난 아이 패드상의 버튼을 클릭하면 54 : 52.784 테스트 [9541 : 1,970,048] * 종료 앱 인해 캐치되지 않는 예외 'NSGenericException'에 이유 : 앱

2014년 9월 25일 14 충돌 '애플리케이션 가 UIAlertController을 제시하고있다() 스타일 UIAlertControllerStyleActionSheet. 의 modalPresentationStyle 및이 스타일의 UIAlertController는 UIModalPresentationPopover입니다. 은 경고 컨트롤러의 popoverPresentationController를 통해이 팝업에 대한 위치 정보를 제공해야합니다. sourceView 및 sourceRect 또는 barButtonItem 중 하나를 으로 제공해야합니다. 알림 컨트롤러를 표시 할 때이 정보가 알 수없는 경우 인 경우 UIPopoverPresentationControllerDelegate 메서드 -prepareForPopoverPresentation에 제공 할 수 있습니다. * 우선 투사 호출 스택 (0 CoreFoundation에서 0x00613df6 exceptionPreprocess 182 + 1 libobjc.A.dylib
0x01fdaa97 objc_exception_throw + 44 2 UIKit
0x0164da37 - [UIPopoverPresentationController presentationTransitionWillBegin] + (3) 3086 UIKit
0x00f54f75 __71- [UIPresentationController _initViewHierarchyForPresentationSuperview :] _ block_invoke + 1,666 4 UIKit 0x00f53554 __56- [UIPresentationController runTransitionForCurrentState] _block_invoke + 226 5 UIKit
0x00f8721b __40 + [UIViewController에 _scheduleTransition :] + _ block_invoke 18 6 UIKit의 0x00e4d62e ___afterCACommitHandler_block_invoke + 15 7 UIKit 0x00e4d5d9 _applyBlockToCFArrayCopiedToStack + 415 8 UIKit
0x00e4d3ee _afterCACommitHandler + 545 9 CoreFoundation에서
0x00536fbe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION
+ 30 10 CoreFoundation에서 0x00536f00 __CFRunLoopDoObservers + 400 11 CoreFoundation에서의 0x0052c93a __CFRunLoopRun + 1,226 12 CoreFoundation에서 0x0052c1ab CFRunLoopRunSpecific + 443 13 CoreFoundation
0x0052bfdb CFRunLoopRunInMode + 123 14 그래픽 서비스
0x0438424f GSEventRunModal + 192 15 GraphicsServices
,0x0438408c GSEventRun + 104 16 UIKit
0x00e23e16 UIApplicationMain 1,526 + 17 시험
0x00085e9e top_level_code + 78 18 테스트
0x00085edb 메인 + 43 19 libdyld.dylib
시작 0x0273eac9 + 1 20 ???
0x00000001이 0x0 + 1)의 libC++ abi.dylib : 다운로드 https://www.dropbox.com/s/54jqd8nsc67ll5g/test.zip?dl=0에서 찾을 시도 할 수 있습니다 유형 NSException

프로젝트의 캐치되지 않는 제외하고 종료.

+0

포스트 내 대답과 아주 비슷합니다 ... 예외는 예외입니다. http://stackoverflow.com/questions/26037657/swift-uiactionsheet-crashes-on-ipad – holex

+0

네, 그래도 여전히 충돌합니다 –

+0

곧 내 대답을 업데이트 할 것입니다 ... – holex

답변

126

경고 메시지가 제대로 표시 될 수 있도록 경고 컨트롤러의 popoverPresentationController 위치를 지정해야한다는 오류 메시지가 표시됩니다. 이것은 간단합니다. popover 컨트롤러가 있는지 확인하고 보낸 사람을 소스로 추가하십시오. 그렇지 않으면

if let popoverController = alertController.popoverPresentationController { 
    popoverController.barButtonItem = sender 
} 
self.presentViewController(alertController, animated: true, completion: nil) 

: 당신의 버튼이 UIBarButtonItem 경우

if let popoverController = alertController.popoverPresentationController { 
    popoverController.sourceView = sender 
    popoverController.sourceRect = sender.bounds 
} 
self.presentViewController(alertController, animated: true, completion: nil) 
+5

swift 1.2, 이제해야 할 일 : popoverController.sourceView = sender as send! UIView – Xerion

+0

'popoverController.sourceRect = sender.bounds'는 iPad의 왼쪽 상단에 ActionSheet를 표시합니다. 대신에'popoverController.sourceRect = sender.frame'을 사용할 수 있습니다. –

+0

'sender.bounds'가 정상적으로 작동합니다 - sourceRect는 'popover를 고정시킬 지정된 뷰의 사각형'입니다. 나는 그것이 sourceView의 좌표 공간에 있다는 것을 의미한다. 따라서 .bounds – leafcutter

6

alertController.popoverPresentationController?.sourceView = self.view 
+0

덕분에, 그것은 충돌로부터 나를 구해 냈지만, 그것은 왼쪽 상단 구석에 보여진다 ... – djdance

1

네이트 요리를 시도 그래서 나는 경우 감지하지만 나는 그것을 할 것입니다 완전히 옳다 iPad 또는 iPhone입니다.

barButtonItem입니다 :

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad){ 

      if let currentPopoverpresentioncontroller = alertController.popoverPresentationController{ 
       currentPopoverpresentioncontroller.barButtonItem = sender as! UIBarButtonItem 
       currentPopoverpresentioncontroller.permittedArrowDirections = UIPopoverArrowDirection.down; 
       self.present(alertController, animated: true, completion: nil) 
      } 
     }else{ 
      self.present(alertController, animated: true, completion: nil) 
     } 
0

VAR actionSheet = UIAlertController (제목 : "카메라 또는 사진 라이브러리를 선택하십시오"메시지 : "", preferredStyle : .actionSheet)

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad){ 
     actionSheet = UIAlertController(title: "Please Select Camera or Photo Library", message: "", preferredStyle: .alert) 
    } 

    actionSheet.addAction(UIAlertAction(title: "Upload a Photo", style: .default, handler: { (UIAlertAction) in 
     self.openPhotoLibrary() 
    })) 
    actionSheet.addAction(UIAlertAction(title: "Take a Photo", style: .default, handler: { (UIAlertAction) in 
     self.openCamera() 
    })) 
    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 
    self.present(actionSheet, animated: true, completion: nil)