iOS 8:UIAlertView / UIAlertController无法显示文本或按钮

11

我有一个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中,它进入循环,因为它找到了alert的子视图。在iOS8中,它说alertView没有子视图。


这是真正的代码吗?如果不是,请发布实际的代码。 - rmaddy
@siddhant 发布真正的代码。这段代码在iOS 8上也可以工作。因此,请在此处复制您在项目中编写的确切代码。 - Jasmeet
尝试这个:http://stackoverflow.com/questions/25927676/uialertview-title-not-display/25928050#25928050 - Anbu.Karthik
Jeev,这是真正的代码。我只是用它来展示点击按钮时的效果。 - Siddhant
我检查了iOS 8中的框架:它给出了(0,0,0,0), 但在iOS 7中,它会给出一个明确的值。 我还检查了uialertview子视图的迭代。在iOS 7中,它进入循环,因为它找到了警报的子视图。在iOS 8中,它会说alertView没有子视图。 - Siddhant
8个回答

29

检查类是否可用

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];

}

12

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代替UIAlertView:

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];

1
这并没有试图回答问题。OP想要一个标题和消息,这在iOS 8下仍然可以使用UIAlertView完美地实现。 - rmaddy

4
我找到了解决方案。问题在于我在项目中使用了UIFont+Replacement类别。这在iOS 7上运行良好,但在iOS 8上使用了一些已弃用的方法。由于这个原因,我不知道为什么,但只有我的警报视图没有显示任何标签。
解决方案:从项目中删除该类别,并通过xib设置字体。一旦我们将任何字体的.tff文件放置在项目工作区中,在自定义字体下,我们就可以看到这些字体名称在xib中。无需使用UIFont+Replacement类别。

它可以工作了:D。非常感谢你,Siddhant。我都快疯了!! - Alex Guerra
1
@AlexGuerra - 不客气。这是一个有点奇怪的问题,甚至还有更奇怪的解决方法 :) - Siddhant

3
请阅读以下代码,了解如何在警告框中添加字段和按钮。

- (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查看我的项目:

IOS中的警报变体


2

iOS 8中,您需要使用UIAlertController 替换 UIAlertViewUIActionSheet。请先阅读苹果论坛中的文档:
苹果Alertcontroller


1
你可以检查那段代码。
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];
}

0
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)

-5

我认为这不是UIAlertview的问题。请检查这个是否正常运作。

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

在任何视图控制器中,请将此代码放在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. 这怎么回答问题呢?楼主说代码在 iOS 7 下可以工作,所以这不是警告显示位置的问题。
- rmaddy
@rmaddy - 在iOS8中仍然显示警报,但没有文本或按钮标签显示。我检查了框架...它给出(0,0,0,0)。 - Siddhant
我已经放置了那个信息。抱歉之前忘记了。 - Siddhant
@Siddhant,你的评论不适用于这个答案。请在问题下方添加你的评论。 - rmaddy
@rmaddy - 完成了。问题现在清楚了吗?请帮忙。 - Siddhant
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接