【问题标题】:PresentViewController is not working in objective c. App is crashingPresentViewController 在目标 c 中不起作用。应用程序崩溃
【发布时间】:2016-06-17 05:32:11
【问题描述】:

当我尝试显示警报视图控制器时,我的应用程序崩溃了。

-(void)setupAlertCtrl{
    self.alertCtrl=[UIAlertController alertControllerWithTitle:@"Select Image"
                                                       message:nil
                                                preferredStyle:UIAlertControllerStyleActionSheet];

}
- (IBAction)selectImagePressed {
    **[self presentViewController:self.alertCtrl
                       animated:YES
                     completion:nil];//this funcanility is breaking**

}

例外

libc++abi.dylib:以 NSException 类型的未捕获异常终止

【问题讨论】:

  • 请分享您的崩溃日志。
  • check 'self' 是指一些视图?

标签: ios objective-c uialertcontroller presentviewcontroller


【解决方案1】:

libc++abi.dylib:以 NSException 类型的未捕获异常终止

如果您将按钮连接到不再存在(或已重命名)的 IBAction,也会发生这种情况"

如果您遇到此问题,请转到您的故事板,右键单击电话大纲顶部的黄色框图标(视图控制器)并删除带有黄色标志的插座。

在这种情况下发生的情况是您可能命名了一个动作,然后重命名了它。

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    这段代码肯定会对你有所帮助。 我怀疑您使用的是 iPad(iPad 中的正常操作表崩溃)

    -(void)setupAlertCtrl
    {        
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {    
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"Your message" preferredStyle:UIAlertControllerStyleActionSheet];
    
            // Remove arrow from action sheet.
            [alertController.popoverPresentationController setPermittedArrowDirections:0];
            //For set action sheet to middle of view.
            CGRect rect = self.view.frame;
            rect.origin.x = self.view.frame.size.width / 20;
            rect.origin.y = self.view.frame.size.height / 20;
            alertController.popoverPresentationController.sourceView = self.view;
            alertController.popoverPresentationController.sourceRect = rect;
            
            [self presentViewController:alertController animated:YES completion:nil];    
        }
        else
        {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"Your message" preferredStyle:UIAlertControllerStyleActionSheet];
            [self presentViewController:alert animated:YES completion:nil];
        }
    
     } 
    

    【讨论】:

      【解决方案3】:

      您必须在视图中调用以下函数并加载

      -(void)setupAlertCtrl
      

      然后您可以在按钮单击时打开警报

      【讨论】:

        【解决方案4】:

        你应该写成这样,

        -(void)setupAlertCtrl
         {
             self.alertCtrl=[UIAlertController alertControllerWithTitle:@"Select Image"
                                                       message:nil
                                                preferredStyle:UIAlertControllerStyleActionSheet];
             [self presentViewController:self.alertCtrl animated:YES completion:nil];
         } 
        
        -(IBAction)selectImagePressed 
        {
            [self setupAlertCtrl];
        }
        

        希望这会对你有所帮助。

        【讨论】:

        • 它在我的最后工作正常,没有任何崩溃,所以请检查你身边关于 IBAction 与故事板中按钮的连接。
        【解决方案5】:

        UIAlertControllerUIAlertViewUIActionSheet 的功能相同、基于块的替代品。通过在创建控制器时设置首选样式来在警报或操作表之间切换。

        UIAlertController *alertController = [UIAlertController
                                      alertControllerWithTitle:alertTitle
                                      message:alertMessage
                                      preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertController animated:YES completion:nil];
        

        希望对你有帮助……

        【讨论】:

          【解决方案6】:

          它没有崩溃, 只需在viewDidLoad 方法中调用SetupAlertCntrl。 它工作正常

          【讨论】:

          • 我认为...您对 alertController 的引用很弱。使该属性强,非原子
          • 它在 presentviewcontroller 上中断
          【解决方案7】:

          你需要先检查你的 alertController 是否存在,如果没有先创建它。还有一件事,UIAlertControl 只能从 UIViewController 呈现,所以请确保 selectImagePressed 函数中的 self 是 UIViewController 的一种。

          - (IBAction)selectImagePressed {
                  if(self.alertController == nil) {
                   [self setupAlertCtrl];
                  }
                  [self presentViewController:self.alertCtrl
                                     animated:YES
                                   completion:nil];//this funcanility is breaking**
          
              }
          

          【讨论】:

          • 请编辑更多信息。不鼓励使用纯代码和“试试这个”的答案,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。我们在这里努力成为知识的资源。
          【解决方案8】:

          试试下面的代码:

          -(void)setupAlertCtrl
          {
              UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Choose Profile Image " message:@" " preferredStyle:UIAlertControllerStyleActionSheet];
          
               UIAlertAction* cameraAction = [UIAlertAction actionWithTitle:@"Take Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                   NSLog(@"Camera Action");
              }];
              [alertController addAction:cameraAction];
          
              UIAlertAction* gallaryAction = [UIAlertAction actionWithTitle:@"Gallary" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                  NSLog(@"Gallary Action");
              }];
              [alertController addAction:gallaryAction];
          
              UIAlertAction* Cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
              [alertController addAction:Cancel];
          
              [self presentViewController:alertController animated:YES completion:nil];
          }
          

          //按钮点击动作

           - (IBAction)selectImagePressed:(id)sender 
             {
          
              [self setupAlertCtrl];
          
             }
          

          希望这会对你有所帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-12-30
            • 2013-08-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多