iOS 8:UIAlertView / UIAlertController 不显示文本或按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26228729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 8: UIAlertView / UIAlertController not showing text or buttons
提问by Siddhant
I have an UIAlertView which is getting shown perfectly in iOS 7 but in iOS 8, it does not show any buttons or labels. Alert is still visible but just a small white box. The OK and cancel buttons take their events as well but no texts are visible.
我有一个 UIAlertView,它在 iOS 7 中完美显示,但在 iOS 8 中,它不显示任何按钮或标签。警报仍然可见,但只是一个小白框。确定和取消按钮也接受它们的事件,但没有文本可见。
I have used this alert to show on click of a 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];
}
I checked the frame in iOS 8: it is giving (0,0,0,0) but in iOS 7 it is giving a definite value.
我检查了 iOS 8 中的框架:它给出了 (0,0,0,0) 但在 iOS 7 中它给出了一个确定的值。
I also checked for iterating into the subviews of uialertview. In iOS7, it goes in the loop, as it finds alert's subviews. In iOS8, it says there are no subviews of alertView.
我还检查了迭代到 uialertview 的子视图。在 iOS7 中,它进入循环,因为它找到警报的子视图。在 iOS8 中,它说没有 alertView 的子视图。
采纳答案by Siddhant
I got the answer to my issue. The issue was that I was using UIFont+Replacement category in my project. This was working fine on iOS 7 but on iOS 8 it was using few deprecated methods. Due to this, I don't know why, but only my alert view was not showing any labels.
我得到了我的问题的答案。问题是我在我的项目中使用了 UIFont+Replacement 类别。这在 iOS 7 上运行良好,但在 iOS 8 上它使用了一些不推荐使用的方法。因此,我不知道为什么,但只有我的警报视图没有显示任何标签。
Solution: Deleted the category from the project and set font through xib. Once we place the .tff file of any font in our project workspace, we see those font names in the xib under custom fonts. NO NEED TO USE UIFont+Replacement category.
解决方法:从项目中删除category,通过xib设置字体。一旦我们将任何字体的 .tff 文件放在我们的项目工作区中,我们就会在自定义字体下的 xib 中看到这些字体名称。无需使用 UIFont+Replacement 类别。
回答by Vijay yadav
Check if the class is available
检查类是否可用
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];
}
回答by Rudra
With iOS 8 you can set the title instead of the message:
在 iOS 8 中,您可以设置标题而不是消息:
[[[UIAlertView alloc] initWithTitle:@"AlertView in iOS 8." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show];
UIAlertView is deprecated from iOS 8 for more information visit this
UIAlertView 已从 iOS 8 弃用以获取更多信息,请访问此
http://nshipster.com/uialertcontroller/. https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/index.html
http://nshipster.com/uialertcontroller/。 https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/index.html
So if you're going to write separate code for iOS 7 and iOS 8, you should be using UIAlertController instead:
因此,如果您要为 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];
回答by DILIP KOSURI
Please read through the code below to understand how to add the fields and buttons to the alerts.
请通读以下代码以了解如何将字段和按钮添加到警报中。
- (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];
}
and if you need a project to completely refer the types of Alerts that are available in IOS, please follow my project from the below URL:
如果您需要一个项目来完全引用 IOS 中可用的警报类型,请从以下 URL 关注我的项目:
回答by Apple Insider
in iOS 8 you need to replace UIAletrview
and UIActionSheet
with UIAlertcontroller
. You read first This documentation in apple forum
Apple Alertcontroller
在 iOS 8 中,您需要将UIAletrview
和替换UIActionSheet
为UIAlertcontroller
. 你先在苹果论坛Apple Alertcontroller阅读本文档
回答by saraman
You can check that code
您可以检查该代码
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];
}
回答by Nrv
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)
回答by Anand
i think this is not UIAlertview
Issues.
please check this work fine..
我认为这不是UIAlertview
问题。请检查这项工作正常..
i think your code issues...........
我认为你的代码问题......
in any view controller
put this code in viewDidLoad
Like below:
在任何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];
}