一、UIAlertController提示框的调用
说明:当我们单独把UIAlertController提示框的写在一个类中时,可以方便我们反复的调用提示框。
具体可以参考:www.tuicool.com/articles/ZJ3Azay
注意:第一点是一开始一直想不通的。
一:因为UIAlertController中有一个语句是
[viewController presentViewController:uia animated:YES completion:nil];
其中的viewController是指视图控制器,所以我们应该把它设置成行为的参数,这样我们就可以传参调用了。
二:在类的头文件声明该行为时,我们可以直接使用 + 号而不是 - 号,
使用加号就是可以直接通过类就可以访问本行为(
[YZBAlert showAlert:self :@"账号不能为空"];
),而不用像 - 号一样要先去创建YZBAlert这个类的行为。
代码如下:
UIAlertController类的头文件:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface YZBAlert : NSObject
+(void)showAlert:(UIViewController *)viewController :(NSString *)msg;//使用加号就是可以直接通过类就可以访问本行为,而不用像 - 号一样要先去创建YZBAlert这个类的行为。
@end
UIAlertController类的实现文件:
#import "YZBAlert.h"
@implementation YZBAlert
+(void)showAlert:(UIViewController *)viewController :(NSString *)msg
{
UIAlertController * uia = [UIAlertController alertControllerWithTitle:@"友情提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * okaction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
}];
[uia addAction:okaction];
[viewController presentViewController:uia animated:YES completion:nil];
}
@end
需要调用时的语法:
步骤
1:导入UIAlertController类的头文件:
2:在需要的地方直接写 [YZBAlert showAlert:self :@"账号不能为空"];