2014-11-23 24 views
0

我想從導航欄的標題中顯示彈出窗口,該窗口已成功配置爲具有多種選擇的按鈕,如iPhone應用程序上的Alert Controller。當輕擊標題時會調用以下代碼,但它會在底部顯示操作表而不是彈出窗口。你能幫我找到問題嗎?謝謝。如何在iPhone上使用iOS 8在彈出窗口中顯示UIAlertController

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil 
                      message: nil 
                    preferredStyle: UIAlertControllerStyleActionSheet]; 
[alertController addAction: [UIAlertAction actionWithTitle:@"Close" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    // dismiss viewcontroller 
    [self dismissViewControllerAnimated:YES completion:nil]; 
}]]; 
[alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    // Handle Cancel 
    // do nothing 
}]]; 

alertController.modalPresentationStyle = UIModalPresentationPopover; 
alertController.preferredContentSize = CGSizeMake(250.0, 180.0); 

UIPopoverPresentationController *popoverPresentation = alertController.popoverPresentationController; 
popoverPresentation.sourceRect = self.parentViewController.navigationItem.titleView.frame; 
popoverPresentation.sourceView = self.view; 
popoverPresentation.permittedArrowDirections = UIPopoverArrowDirectionUp; 
popoverPresentation.delegate = self; 

[self presentViewController: alertController animated: YES completion: nil]; 

回答

0

你不能用代碼(編程)與UIAlertController做到這一點,因爲它具有在iPhone上沒有popoverPresentationController。它僅在iPad中可用。

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:PDFDataArray applicationActivities:nil]; 

if ([activityVC respondsToSelector:@selector(popoverPresentationController)]) { 
    NSLog(@"Popover available for this device type"); 
}  

我已經成功地呈現在iPhone popovers在過去,使用自適應賽格瑞風格Present As Popover接口建設者/分鏡和實施adaptivePresentationStyleForPresentationController委託方法返回正確的UIModalPresentationStyle。你也可以嘗試這種方法。