弹窗提醒的实现

342 阅读1分钟

UIAlertController

  1. 实例化方法

title : 一般设置为需要显示的弹窗消息

message : title下面显示的字体较小的信息,一般为nil

UIAlertControllerStyle : 弹窗展示的样式,actionSheet表示底部弹出的弹窗,alert表示中间弹出的弹窗


[UIAlertController alertControllerWithTitle:<#(nullable NSString *)#> message:<#(nullable NSString *)#>preferredStyle:<#(UIAlertControllerStyle)#>];

  1. 添加控件

UIAlertAction 是UIAlertController的子控件,为UIAlertController实现按钮点击功能

title : 表示点击按钮的文字

style : 表示点击按钮的样式,注意在同一个AlertController中只允许同时存在一个cancel,否则程序会报错

handler : 点击按钮时执行的代码块


[UIAlertActionaction WithTitle:<#(nullable NSString *)#> style:<#(UIAlertActionStyle)#>handler:<#^(UIAlertAction * _Nonnull action)handler#>]

  1. 展示弹窗

self : 当前控制器

controller : 要显示的弹窗对象

animated : 是否带有动画效果

completion : block代码块,一般传nil


[self presentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#>completion:<#^(void)completion#>]

被废弃的UIAlertView


- (void) show {

      UIAlertView*alertView =[[UIAlertView alloc] initWithTitle:@"warning"message:@""delegate:nilcancelButtonTitle:@"cancel"otherButtonTitles:@"sure",nil];    

     [alertView show];

}