2016-11-23 109 views
0

我正在使用objective-c寫入關於UIAlertControllerStyleActionSheetUIAlertcontrolleriOS popover顯示兩次會崩潰

我想在iPhone上顯示警報表,並在iPad上顯示popoverPresentationController

首先,我已經設置了UIPopoverPresentationControllerDelegate委託。

當我點擊我的按鈕時,彈出窗口顯示正確。

但我點擊屏幕關閉彈出。它會顯示下面的警告。

[警告] < _UIPopoverBackgroundVisualEffectView 0x14be52ef0>被要求爲其不透明度設置動畫。這將導致效果顯示中斷,直到不透明度返回到1.

現在當我單擊按鈕時再次顯示彈出視圖。

它會在日誌下面顯示崩潰。

由於未捕獲的異常「NSGenericException」而終止應用程序,原因:'您的應用程序提供了樣式爲UIAlertControllerStyleActionSheet的UIAlertController()。具有此樣式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover。您必須通過警報控制器的popoverPresentationController爲此彈出窗口提供位置信息。您必須提供sourceView和sourceRect或barButtonItem。如果在提供警報控制器時未知此信息,則可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供該信息。 ***第一擲調用堆棧: (0x18d9a41c0 0x18c3dc55c 0x19418a8b0 0x193ac60a8 0x193ac3df4 0x193a08d0c 0x1939faac0 0x19376a22c 0x18d9517dc 0x18d94f40c 0x18d94f89c 0x18d87e048 0x18f2ff198 0x1937e2b50 0x1937dd888 0x10011198c 0x18c8605b8) 的libC++ abi.dylib:與類型的未捕獲的異常終止NSException

有誰知道如何解決這個問題?

我的代碼如下:

@interface ViewController()  <...UITextViewDelegate,UITextFieldDelegate...> { 
     UIAlertController *alertTypeAlertController; 
     UIAlertAction *alertType1Action; 
     UIAlertAction *alertType2Action; 
     UIPopoverPresentationController *popPresenter; 
    } 

- (void)viewDidLoad { 
     [super viewDidLoad]; 


    alertTypeAlertController = [UIAlertController 
          alertControllerWithTitle:@"selecte one:" 
          message:nil 
          preferredStyle:UIAlertControllerStyleActionSheet]; 

alertType1Action = [UIAlertAction 
        actionWithTitle:@"Type1" 
        style:UIAlertActionStyleDefault 
        handler:nil]; 
alertType2Action = [UIAlertAction 
        actionWithTitle:@"Type2" 
        style:UIAlertActionStyleDefault 
        handler:nil]; 
    [alertTypeAlertController addAction: alertType1Action]; 
    [alertTypeAlertController addAction: alertType2Action]; 

    // for ipad 
    popPresenter = [alertTypeAlertController             popoverPresentationController]; 

    popPresenter.permittedArrowDirections = UIPopoverArrowDirectionLeft; 

    popPresenter.delegate = self; 
    popPresenter.sourceView = self.theTypeBtn;      
    popPresenter.sourceRect = CGRectMake(230, 22, 10, 10); 

    .... 
    } 

    - (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController { 
// called when a Popover is dismissed 
    } 

    - (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController { 

// return YES if the Popover should be dismissed 
// return NO if the Popover should not be dismissed 
return YES; 
    } 

    -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller { 

return UIModalPresentationNone; 
    } 
enter code here 

非常感謝你。

回答

2

也許UIAlertControllerUIPopoverPresentationController對象被ViewController對象強烈引用,這使得Popover無法在釋放它後釋放該對象 。


後來我發現你的問題是,你嘗試在viewDidLoad方法來創建popPresenter 一次,每次你觸摸 按鈕時出示它,你應該創建一個新的來代替,你可以移動viewDidLoad中代碼的新方法,並通過觸摸事件調用它,解決這樣的:

- (void)makePopover 
{ 
    alertTypeAlertController = [UIAlertController 
           alertControllerWithTitle:@"selecte one:" 
           message:nil 
           preferredStyle:UIAlertControllerStyleActionSheet]; 

    alertType1Action = [UIAlertAction 
         actionWithTitle:@"Type1" 
         style:UIAlertActionStyleDefault 
         handler:nil]; 
    alertType2Action = [UIAlertAction 
         actionWithTitle:@"Type2" 
         style:UIAlertActionStyleDefault 
         handler:nil]; 
    [alertTypeAlertController addAction: alertType1Action]; 
    [alertTypeAlertController addAction: alertType2Action]; 

    // for ipad 
    popPresenter = [alertTypeAlertController             popoverPresentationController]; 

    popPresenter.permittedArrowDirections = UIPopoverArrowDirectionLeft; 
    popPresenter.canOverlapSourceViewRect = YES; // adding this line 
    popPresenter.delegate = self; 
    popPresenter.sourceView = self.theTypeBtn; 
    popPresenter.sourceRect = CGRectMake(230, 22, 10, 10); 
} 
- (IBAction)touchButton:(id)sender { 
    [self makePopover]; 
    [self presentViewController:alertTypeAlertController animated:YES completion:nil]; 
} 
+0

我試試「UIPopoverPresentationController * popPresenter」,結果仍然顯示了同樣的錯誤之前添加__weak。:( – dickfala

+0

檢查第二個彈出如果self.presentedViewController是零,正常的應用程序崩潰,如果你試圖呈現2查看控制器 –

+0

我查看第一個點擊按鈕,彈出窗口是零(self.presentedViewController),但是顯示正確,應用程序沒有崩潰,但點擊第二個按鈕,彈出窗口仍然爲零,應用程序崩潰。@@ – dickfala

0

我只是修改代碼,請檢查其是否工作或沒有。

- (IBAction)actionButton:(UIButton*)sender { 
     alertTypeAlertController = [UIAlertController 
          alertControllerWithTitle:@"selecte one:" 
          message:nil 
          preferredStyle:UIAlertControllerStyleActionSheet]; 

     alertType1Action = [UIAlertAction 
        actionWithTitle:@"Type1" 
        style:UIAlertActionStyleDefault 
        handler:nil]; 
     alertType2Action = [UIAlertAction 
        actionWithTitle:@"Type2" 
        style:UIAlertActionStyleDefault 
        handler:nil]; 
     [alertTypeAlertController addAction: alertType1Action]; 
     [alertTypeAlertController addAction: alertType2Action]; 

    // for ipad 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
     popPresenter = [alertTypeAlertController             popoverPresentationController];  
     popPresenter.permittedArrowDirections = UIPopoverArrowDirectionLeft; 

     popPresenter.delegate = self; 
     popPresenter.sourceView = self.theTypeBtn;      
     popPresenter.sourceRect = CGRectMake(230, 22, 10, 10); 
    } 
    [self presentViewController:alertTypeAlertController animated:YES completion:nil]; 
    } 
+0

我試試這個方法,如果我舔按鈕,它會崩潰。日誌:***終止應用程序,由於未捕獲異常'NSInvalidArgumentException',原因:'應用程序試圖在目標上呈現一個無模式視圖控制器。 – dickfala