UIAlertController 中的选择器 presentViewController 没有已知的 class 方法
No known class method for selector presentViewController in UIAlertController
我知道这是一个非常简单的问题,我已经研究了 3 个小时,但没能成功。我正在尝试实现 UIAlertController 以使用 Apple documentation 显示错误消息,但我在这一行收到错误消息,即选择器 presentViewController [self presentViewController:alert animated:YES completion:nil];
没有已知的 class 方法我搜索并获得了很多解决方案,但是 none 在这里工作。 AlertMessageViewController是我自定义的class,继承自UIViewController
AlertMessageViewController.h
#import <UIKit/UIKit.h>
@interface AlertMessageViewController : UIViewController
+(instancetype)showAlert: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle;
@end
AlertMessageViewController.m
#import "AlertMessageViewController.h"
#import <UIKit/UIKit.h>
@interface AlertMessageViewController ()
@end
@implementation AlertMessageViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
+(instancetype)showAlert: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"alertMessage" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok =[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){NSLog(@"ok action");}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
@end
为什么您的 return 类型是 showAlert: method instancetype?而你什么都没有return,应该是无效的。
编辑:另外,你的方法不应该是Class方法
这应该有效:
-(void)showAlert: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"alertMessage" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok =[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){NSLog(@"ok action");}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
更新:
好的试试这个:
+(UIAlertController*)alertWithTitle: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle: preferredStyle];
UIAlertAction *ok =[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){NSLog(@"ok action");}];
[alert addAction:ok];
return alert;
}
}
然后当你想展示它的时候,这样调用它:
[self presentViewController:[AlertMessageViewController alertWithTitle:@"Title" withMessage:@"Message" preferredStyle:UIAlertControllerStyleAlert] animated:YES completion:NULL];
我知道这是一个非常简单的问题,我已经研究了 3 个小时,但没能成功。我正在尝试实现 UIAlertController 以使用 Apple documentation 显示错误消息,但我在这一行收到错误消息,即选择器 presentViewController [self presentViewController:alert animated:YES completion:nil];
没有已知的 class 方法我搜索并获得了很多解决方案,但是 none 在这里工作。 AlertMessageViewController是我自定义的class,继承自UIViewController
AlertMessageViewController.h
#import <UIKit/UIKit.h>
@interface AlertMessageViewController : UIViewController
+(instancetype)showAlert: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle;
@end
AlertMessageViewController.m
#import "AlertMessageViewController.h"
#import <UIKit/UIKit.h>
@interface AlertMessageViewController ()
@end
@implementation AlertMessageViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
+(instancetype)showAlert: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"alertMessage" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok =[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){NSLog(@"ok action");}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
@end
为什么您的 return 类型是 showAlert: method instancetype?而你什么都没有return,应该是无效的。
编辑:另外,你的方法不应该是Class方法
这应该有效:
-(void)showAlert: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"alertMessage" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok =[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){NSLog(@"ok action");}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
更新: 好的试试这个:
+(UIAlertController*)alertWithTitle: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle: preferredStyle];
UIAlertAction *ok =[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){NSLog(@"ok action");}];
[alert addAction:ok];
return alert;
}
}
然后当你想展示它的时候,这样调用它:
[self presentViewController:[AlertMessageViewController alertWithTitle:@"Title" withMessage:@"Message" preferredStyle:UIAlertControllerStyleAlert] animated:YES completion:NULL];