2015-10-29 14 views
0

我使用下面的代碼爲警報視圖,它在iPhone中工作。但是當我在iPad上運行它,它給了以下錯誤:警報視圖的UIAlertController中的UIPickerView僅在iPhone中正確時在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.'

代碼如下:

let alertView = UIAlertController(title: "Select Launguage", message: "\n\n\n\n\n\n\n\n\n\n", preferredStyle: UIAlertControllerStyle.ActionSheet); 

    pickerView.center.x = self.view.center.x 

    alertView.view.addSubview(pickerView) 

    let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil) 

    alertView.addAction(action) 
    presentViewController(alertView, animated: true, completion: nil) 

請幫助我。爲什麼它不適用於iPad ...

+0

這裏看你需要做什麼:http://stackoverflow.com/questions/24224916/presenting-a-uialertcontroller-properly-on-an-ipad-using-ios-8 – totiG

+0

我嘗試一個之前。但只在iPad上工作。不在iPhone上工作。 –

回答

0

在iPad上,警報將顯示爲使用UIPopoverPresentationController的彈出窗口,它要求您指定彈出窗口演示的錨點。

UIViewController *self; // code assumes you're in a view controller 
UIButton *button; // the button you want to show the popup sheet from 

UIAlertController *alertController; 
UIAlertAction *destroyAction; 
UIAlertAction *otherAction; 

alertController = [UIAlertController alertControllerWithTitle:nil 
                 message:nil 
          preferredStyle:UIAlertControllerStyleActionSheet]; 
destroyAction = [UIAlertAction actionWithTitle:@"Remove All Data" 
             style:UIAlertActionStyleDestructive 
             handler:^(UIAlertAction *action) { 
              // do destructive stuff here 
             }]; 
otherAction = [UIAlertAction actionWithTitle:@"Blah" 
             style:UIAlertActionStyleDefault 
            handler:^(UIAlertAction *action) { 
             // do something here 
            }]; 
// note: you can control the order buttons are shown, unlike UIActionSheet 
[alertController addAction:destroyAction]; 
[alertController addAction:otherAction]; 
[alertController setModalPresentationStyle:UIModalPresentationPopover]; 

UIPopoverPresentationController *popPresenter = [alertController 
               popoverPresentationController]; 
popPresenter.sourceView = button; 
popPresenter.sourceRect = button.bounds; 
[self presentViewController:alertController animated:YES completion:nil]; 
相關問題