我有错误:'UIAlertController'没有可见的界面

时间:2016-06-09 13:57:43

标签: ios objective-c cocoa-touch uikit uialertcontroller

我是Xcode(7.3)和ios(9.3)的新手。我尝试了一个示例项目来显示警告信息,但我得到的错误如下:

“没有'UIAlertController的可见界面'声明'show'。我在附上代码。

//ViewController.m//

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                                                         message:@"This is an alert."
                                                       preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {}];
[alert show];


//AppDelegate.h//


@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@end.

请帮帮我。

3 个答案:

答案 0 :(得分:1)

尝试改为:

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                                                     message:@"This is an alert."
                                                   preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                  handler:^(UIAlertAction * action) {}];

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

答案 1 :(得分:1)

在创建UIAlertController时,您必须编写以下代码:

[self presentViewController:alert animated:YES completion:nil];

而不是

 [alert show];

有关UIAlertController的详细信息,请参阅:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/

答案 2 :(得分:0)

你做错了。 show仅适用于UIAlertVIew,但不适用于UIAlertAction

UIALertAction是添加到UIAlertController

的操作