iOS 8:UIAlertView / UIAlertController不显示文本或button

我有一个UIAlertView在iOS 7中完美显示,但在iOS 8中,它不显示任何button或标签。 警报仍然可见,但只是一个小白盒子。 “确定”和“取消”button也将其事件作为参考,但不会显示任何文本。

我已经使用这个警报来显示一个button

- (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的子视图。

检查课程是否可用

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

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

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

UIAlertView已从iOS 8中弃用,以获取更多信息

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

我得到了我的问题的答案。 问题是我在我的项目中使用了UIFont + Replacement类。 这在iOS 7上工作正常,但在iOS 8上使用了几个不推荐使用的方法。 由于这个原因,我不知道为什么,但只有我的警报视图没有显示任何标签。

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

请仔细阅读下面的代码,了解如何将字段和button添加到警报。

 - (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 8中,您需要用UIAletrviewreplaceUIAletrviewUIActionSheet 。 您首先阅读本文档在苹果论坛
Apple Alertcontroller

您可以检查该代码

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

我认为这不是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]; }