【问题标题】:iOS 8: UIAlertView / UIAlertController not showing text or buttonsiOS 8:UIAlertView / UIAlertController 不显示文本或按钮
【发布时间】:2014-10-07 04:49:02
【问题描述】:

我有一个 UIAlertView,它在 iOS 7 中完美显示,但在 iOS 8 中,它不显示任何按钮或标签。警报仍然可见,但只是一个小白框。 OK 和 cancel 按钮也接受它们的事件,但没有文本可见。

我已使用此警报在单击按钮时显示

- (IBAction)sel_btnLogout:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Logout!" message:@"Are you sure you want to logout?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    [alert show];
}

我在 iOS 8 中检查了框架:它给出了 (0,0,0,0) 但在 iOS 7 中它给出了一个确定的值。

我还检查了迭代到 uialertview 的子视图。在 iOS7 中,它进入循环,因为它找到警报的子视图。在iOS8中,它说没有alertView的子视图。

【问题讨论】:

  • 这是真正的代码吗?如果不是,请发布实际代码。
  • @siddhant 发布真实代码。此代码也适用于 iOS 8。因此,将您在项目中编写的确切代码复制到此处。
  • Jeev,这是真正的代码。我刚刚使用它来显示单击按钮。
  • 我检查了 iOS 8 中的框架:它给出了 (0,0,0,0) 但在 iOS 7 中它给出了一个确定的值。我还检查了迭代到 uialertview 的子视图。在 iOS7 中,它进入循环,因为它找到警报的子视图。在 iOS8 中,它说没有 alertView 的子视图。

标签: ios objective-c ios8 uialertview


【解决方案1】:

检查课程是否可用

if ([UIAlertController class])
{

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert title" message:@"Alert message" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:ok];

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

} 
else 
{

    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Alert title" message:@"Alert message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alert show];

}

【讨论】:

    【解决方案2】:

    在 iOS 8 中,您可以设置标题而不是消息:

    [[[UIAlertView alloc] initWithTitle:@"AlertView in iOS 8." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show];
    

    iOS 8 已弃用 UIAlertView 以了解更多信息,请访问此

    http://nshipster.com/uialertcontroller/。 https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/index.html

    因此,如果您要为 iOS 7 和 iOS 8 编写单独的代码,则应该改用 UIAlertController:

    UIAlertController *alertController = [UIAlertController  alertControllerWithTitle:@"AlertView in iOS 8"  message:nil  preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
     [self dismissViewControllerAnimated:YES completion:nil];
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
    

    【讨论】:

    • 这并不试图回答这个问题。 OP 想要一个标题和消息,在 iOS 8 下使用 UIAlertView 仍然很好。
    【解决方案3】:

    我得到了我的问题的答案。问题是我在项目中使用了 UIFont+Replacement 类别。这在 iOS 7 上运行良好,但在 iOS 8 上它使用了一些不推荐使用的方法。因此,我不知道为什么,但只有我的警报视图没有显示任何标签。

    解决方法:从项目中删除类别并通过xib设置字体。一旦我们将任何字体的 .tff 文件放在我们的项目工作区中,我们就会在自定义字体下的 xib 中看到这些字体名称。无需使用 UIFont+替换类别。

    【讨论】:

    • 它有效:D。非常感谢你,悉丹。我快疯了!
    • @AlexGuerra - 欢迎您。有点奇怪的问题,甚至更奇怪的解决方案:)
    【解决方案4】:

    请通读下面的代码以了解如何将字段和按钮添加到警报中。

    - (IBAction)UIAlertControllerWithActionSheetTextFields:(id)sender {
        UIAlertController * alert=   [UIAlertController
                                      alertControllerWithTitle:@"Info"
                                      message:@"You are using UIAlertController with Actionsheet and text fields"
                                      preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* ok = [UIAlertAction
                             actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                                 NSLog(@"Resolving UIAlert Action for tapping OK Button");
                                 [alert dismissViewControllerAnimated:YES completion:nil];
                                 
                             }];
        UIAlertAction* cancel = [UIAlertAction
                                 actionWithTitle:@"Cancel"
                                 style:UIAlertActionStyleDefault
                                 handler:^(UIAlertAction * action)
                                 {
                                     NSLog(@"Resolving UIAlertActionController for tapping cancel button");
                                     [alert dismissViewControllerAnimated:YES completion:nil];
                                     
                                 }];
        
        [alert addAction:ok];
        [alert addAction:cancel];
        
        [alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
           textField.accessibilityIdentifier = @"usernameTextField";
            textField.placeholder = @"Enter your username";
            textField.accessibilityLabel = @"usernameLabel";
        }];
        
        [alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
           textField.placeholder = @"Enter your password";
           textField.accessibilityIdentifier = @"paswordTextField";
           textField.accessibilityLabel = @"passwordLabel";
        }];
        
        [self presentViewController:alert animated:YES completion:nil];
        
    }

    如果您需要一个项目来完整引用 IOS 中可用的警报类型,请从以下 URL 关注我的项目:

    Alert variations in IOS

    【讨论】:

      【解决方案5】:

      在 iOS 8 中,您需要将 UIAletrview 和 UIActionSheet 替换为 UIAlertcontroller 。您先阅读 Apple 论坛中的此文档
      Apple Alertcontroller

      【讨论】:

        【解决方案6】:

        您可以检查该代码

        if (([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] == NSOrderedAscending))
        {
            // use UIAlertView
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:Title message:desc delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
        else {
            // use UIAlertController
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:Title message:desc preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
            }];
            [alert addAction:okAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        

        【讨论】:

          【解决方案7】:
          let alert = UIAlertController(title: "Warning" as String , message: messageString as String, preferredStyle: .Alert)
                  let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default)
                  {
                      UIAlertAction in
                          self.dismissViewControllerAnimated(true, completion: nil)
                  }
                  // Add the actions
                  alert.addAction(okAction)
                  self.presentViewController(alert, animated: true, completion: nil)
          

          【讨论】:

            【解决方案8】:

            我认为这不是UIAlertview 问题。 请检查这项工作是否正常..

            我认为你的代码有问题............

            在任何view controller 中将此代码放入viewDidLoad 中,如下所示:

            - (void)viewDidLoad 
            {
                [super viewDidLoad];
            
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"My message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
                [alert show];
            }
            

            【讨论】:

            • 1) 不显示来自viewDidLoad 的警报。该视图尚未显示。 2)这如何回答这个问题? OP 声明代码在 iOS 7 下工作,因此显示警报的位置不是问题。
            • @rmaddy - iOS8 中仍然显示警报,但没有显示文本或按钮标签。我检查了框架......它给出了(0,0,0,0)
            • @Siddhant 把这些信息放在你的问题中,而不是这个不相关的答案。
            • 我已经输入了该信息。抱歉忘记早点了。
            • @rmaddy - 请查看我的更新信息。我检查了 iOS7 和 iOS 8 中 UIAlertView 的层次结构。在 iOS7 中,它显示子视图,但在 iOS8 中,它不显示任何子视图。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-02
            • 1970-01-01
            • 2014-11-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多