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

时间:2014-10-07 04:49:02

标签: ios objective-c ios8 uialertview

我有一个UIAlertView,它在iOS 7中完美呈现,但在iOS 8中,它没有显示任何按钮或标签。警报仍然可见,但只是一个小白框。 “确定”和“取消”按钮也会显示其事件,但不会显示任何文本。

我已经使用此提醒显示点击按钮

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

8 个答案:

答案 0 :(得分: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 :(得分: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];

答案 2 :(得分:4)

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

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

答案 3 :(得分: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

答案 4 :(得分:2)

在iOS 8中,您需要将UIAletrviewUIActionSheet替换为UIAlertcontroller。您先阅读本文档在apple forum中 Apple Alertcontroller

答案 5 :(得分: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];
}

答案 6 :(得分: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)

答案 7 :(得分:-5)

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