2014-10-07 104 views
11

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

我已经使用这个警报就按一下按钮显示

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

+0

这是真正的密码?如果不是,请发布实际代码。 – rmaddy 2014-10-07 04:52:40

+0

@siddhant发布真实的代码。此代码也在iOS 8中工作。因此,请在此复制您在项目中编写的确切代码。 – Jasmeet 2014-10-07 04:53:36

+0

试试这个http://stackoverflow.com/questions/25927676/uialertview-title-not-display/25928050#25928050 – 2014-10-07 04:59:29

回答

4

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

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

+0

它的工作原理:D。 Siddhant非常感谢你。我疯了! – 2014-10-21 15:51:08

+1

@AlexGuerra - 不客气。有点奇怪的问题,甚至更奇怪的解决方案:) – Siddhant 2014-10-24 08:50:26

-5

我认为这不是UIAlertview问题。 请把这个代码viewDidLoad像下面任何view controller检查这项工作很好..

我觉得你的代码问题...........

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"My message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; 
    [alert show]; 
} 
+0

1)不要显示来自'viewDidLoad'的警报。该视图尚未显示。 2)这是如何回答这个问题的? OP声明代码在iOS 7下工作,所以它不会显示警报的位置。 – rmaddy 2014-10-07 05:02:21

+0

@rmaddy - Alert仍然显示在iOS8中,但没有显示任何文本或按钮标签。我检查了框架......它给(0,0,0,0) – Siddhant 2014-10-07 05:26:20

+0

@Siddhant把你的问题的信息,而不是这个无关的答案。 – rmaddy 2014-10-07 05:29:00

2

在iOS 8中,您需要用UIAlertcontroller替换UIAletrviewUIActionSheet。您先阅读在苹果论坛本文档
Apple Alertcontroller

12

与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]; 
+1

这不会尝试回答这个问题。 OP需要一个标题和消息,在iOS 8下使用'UIAlertView'仍然非常好。 – rmaddy 2014-10-07 05:15:55

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

} 
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]; 
} 
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中可用的类型的警报,请按照我的项目从以下网址:

Alert variations in IOS

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)