【问题标题】:iOS: Strange error...UIAlertView not showing...even though it get's callediOS:奇怪的错误...UIAlertView 没有显示...即使它被调用
【发布时间】:2014-03-11 09:11:06
【问题描述】:

我不知道我的错误在哪里......我已经在我的应用程序的其他视图控制器中使用了(!)这些代码行并且它有效。我已经设置了断点,如果正确执行并调用了 [warning show],但什么也没做!这是我的 viewWillAppear 方法中的第一行(在 [super viewWillAppear... 之后)。

if([self.category isDeleted] || ![((AppDelegate *)[[UIApplication sharedApplication] delegate]).managedObjectContext existingObjectWithID:self.category.objectID error:NULL]){
        UIAlertView *warning = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"CategoryDeletedError", nil) message:NSLocalizedString(@"CategoryDeletedErrorExplanation", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil, nil];
        [warning show];
        NSLog(@"%@",warning);
    }

NSLog 打印出这个:

2014-03-11 10:08:08.133 App[752:70b] <UIAlertView: 0x8a8bb20; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x8aa2d60>>

所以创建了对象..但是为什么它不起作用?我的视图控制器也是正确的 UIAlertViewDelegate...

【问题讨论】:

    标签: ios objective-c uialertview


    【解决方案1】:

    框架是frame = (0 0; 0 0);,所以你看不到。也许NSLocalizedString 返回空字符串。尝试使用 @"CategoryDe​​letedError" 设置标题;如果显示,请检查项目中的本地化字符串文件。

    【讨论】:

      【解决方案2】:

      知道了...刚发布后我就知道了。警告将在我的 viewWillAppear 方法之后显示。但是,在这个 if 子句之后有一行代码调用已删除的实体并使应用程序崩溃。因此,我不只是在 if 子句中写这个,而是这样解决它:

      之前:

      if([self.category isDeleted] || ![((AppDelegate *)[[UIApplication sharedApplication] delegate]).managedObjectContext existingObjectWithID:self.category.objectID error:NULL]){
              UIAlertView *warning = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"CategoryDeletedError", nil) message:NSLocalizedString(@"CategoryDeletedErrorExplanation", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil, nil];
              [warning show];
              NSLog(@"%@",warning);
          }
      
      Rest of the code...crashing
      

      现在:

      if([self.category isDeleted] || ![((AppDelegate *)[[UIApplication sharedApplication] delegate]).managedObjectContext existingObjectWithID:self.category.objectID error:NULL]){
              UIAlertView *warning = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"CategoryDeletedError", nil) message:NSLocalizedString(@"CategoryDeletedErrorExplanation", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil, nil];
              [warning show];
              NSLog(@"%@",warning);
          } else {
             Rest of the code
          }
      

      【讨论】:

        【解决方案3】:

        试试看

        if([self.category isDeleted] || ![((AppDelegate *)[[UIApplication sharedApplication] delegate]).managedObjectContext existingObjectWithID:self.category.objectID error:NULL])
        {
        
        dispatch_async(dispatch_get_main_queue(), ^{
                           UIAlertView *warning = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"CategoryDeletedError", nil) message:NSLocalizedString(@"CategoryDeletedErrorExplanation", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil, nil];
                [warning show];
                NSLog(@"%@",warning);
                         });
        
        
           }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-28
          • 2016-06-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多