UIAlertController 与 NSObject 重用更多次
UIAlertController reuse more times with NSObject
在我的应用程序中,UIAlertController 使用了很多 time.So,我创建了 NSObject
Helper.
这里是Helper.h
#import <Foundation/Foundation.h>
#import "PrefixHeader.pch"
#import "LectureViewController.h"
@interface Helper : NSObject
+(void) showNotice:(NSString *) message;
+(void) showNoticeWithAction;
@end
这里是Helper.m
#import "Helper.h"
#import "AppDelegate.h"
@implementation Helper
+(void) showNotice:(NSString *) message{
NSArray *versionArray = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
if ([[versionArray objectAtIndex:0] intValue] >= 9) {
//ver.IOS >= 9
UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleAlert];
alert.view.backgroundColor = [UIColor grayColor];
// [self presentViewController:alert animated:YES completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:^{
}];
});
} else {
//ver.IOS < 9
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[alert performSelector:@selector(show) withObject:nil afterDelay:5];
}
}
@end
由于 presentViewController
,此行不能 运行 在助手中:[self presentViewController:alert animated:YES completion:nil];
。请帮助我
您收到此错误是因为基础 class 是 NSObject 并且 self
表示该 class 的对象,而 NSObject 没有任何名为 presentViewController 的方法。
要解决它,您需要在该方法中传递一个 UIViewController 对象,并在该对象上像这样使用 presentViewController
+(void) showNotice:(NSString *) message andViewController : (UIViewController *) controller;
然后像这样使用它
[controller presentViewController:alert animated:YES completion:nil];
在您的控制器被按下并显示时尝试此操作。
UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[[vc presentedViewController] ? vc.presentedViewController : vc presentViewController:alert animated:YES completion:nil];
在 class nsobject
的@end 中创建 c 函数的击球手方式
void showAlertView( NSString *title,NSString *message) {
[[[UIAlertController alloc]initWithTitle:title message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil] show];
}
并像这样调用任何 class
showAlertView(@"Done", [NSString stringWithFormat:@"%@",responseObject[@"message"]]);
在我的应用程序中,UIAlertController 使用了很多 time.So,我创建了 NSObject
Helper.
这里是Helper.h
#import <Foundation/Foundation.h>
#import "PrefixHeader.pch"
#import "LectureViewController.h"
@interface Helper : NSObject
+(void) showNotice:(NSString *) message;
+(void) showNoticeWithAction;
@end
这里是Helper.m
#import "Helper.h"
#import "AppDelegate.h"
@implementation Helper
+(void) showNotice:(NSString *) message{
NSArray *versionArray = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
if ([[versionArray objectAtIndex:0] intValue] >= 9) {
//ver.IOS >= 9
UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleAlert];
alert.view.backgroundColor = [UIColor grayColor];
// [self presentViewController:alert animated:YES completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:^{
}];
});
} else {
//ver.IOS < 9
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[alert performSelector:@selector(show) withObject:nil afterDelay:5];
}
}
@end
由于 presentViewController
,此行不能 运行 在助手中:[self presentViewController:alert animated:YES completion:nil];
。请帮助我
您收到此错误是因为基础 class 是 NSObject 并且 self
表示该 class 的对象,而 NSObject 没有任何名为 presentViewController 的方法。
要解决它,您需要在该方法中传递一个 UIViewController 对象,并在该对象上像这样使用 presentViewController
+(void) showNotice:(NSString *) message andViewController : (UIViewController *) controller;
然后像这样使用它
[controller presentViewController:alert animated:YES completion:nil];
在您的控制器被按下并显示时尝试此操作。
UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[[vc presentedViewController] ? vc.presentedViewController : vc presentViewController:alert animated:YES completion:nil];
在 class nsobject
的@end 中创建 c 函数的击球手方式void showAlertView( NSString *title,NSString *message) {
[[[UIAlertController alloc]initWithTitle:title message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil] show];
}
并像这样调用任何 class
showAlertView(@"Done", [NSString stringWithFormat:@"%@",responseObject[@"message"]]);