OC自带的Alert弹窗

一,UIAlertController的使用:

UIAlertController是一个控制器,所以需要按照控制器的方式进行显示:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion;

手动销毁的时候也需要按照控制器的方式进行退出:
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion

下面将详细介绍一下这个控制器的使用:

1,创建控制器

//在这里控制alert的样式是UIAlertControllerStyleAlert还是UIAlertControllerStyleActionSheet
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"alertController" message:@"This is an alertController" preferredStyle:UIAlertControllerStyleAlert];

2,添加按钮,并为按钮设置样式//可以添加很多个,随意,通过后面的block进行回调,无需代理方法

//这一步是为了防止循环引用
__weak typeof(alertController) weakAlert = alertController;

UIAlertAction *actionDefault = [UIAlertAction actionWithTitle:@"action Default" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"action Default did clicked");
    //为了防止循环引用,在这里需要进行弱引用设置,下面如果有用到alertController的也需要做相同的处理
    NSString *text = weakAlert.textFields.lastObject.text;
    NSLog(@"%@",text);
}];
[alertController addAction:actionDefault];

UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"action cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"action cancel did clicked");
}];
[alertController addAction:actionCancel];

UIAlertAction *actionDestructive = [UIAlertAction actionWithTitle:@"action destructive" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"action destructive did clicked");
}];
[alertController addAction:actionDestructive];

UIAlertAction *actionDestructive01 = [UIAlertAction actionWithTitle:@"action destructive 01" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"action destructive 01 did clicked");
}];
[alertController addAction:actionDestructive01];

3,添加文本框//可以添加很多个,随意,文本框的属性可以在后面的block中设置

[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    textField.text = @"zhangdanfeng";
}];

4,显示控制器

[self presentViewController:alertController animated:YES completion:^{
    
}];

二,UIAlertView的使用:

1,创建view:

//你可以在创建的时候决定上面有多少个按钮
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is an alert" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"Are you sure?",@"ddd",@"fad", nil];
//创建后如果想要增加按钮也可以:(按钮如果太多,会通过scroll方式展示)
[alertView addButtonWithTitle:@"zhhhhhh"];

2,设置UIAlertView样式:(共有四种样式)


//    UIAlertViewStyleDefault = 0,
//    UIAlertViewStyleSecureTextInput,
//    UIAlertViewStylePlainTextInput,
//    UIAlertViewStyleLoginAndPasswordInput
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

3,设置代理<UIAlertViewDelegate>,然后展示出来

//其实在创建的时候已经设置过代理了,这里不需要再重新设置了
//    alertView.delegate = self;
[alertView show];

4,实现代理方法:

//代理方法

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%ld",(long)buttonIndex);
}

// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)alertViewCancel:(UIAlertView *)alertView{
    NSLog(@"break");
}

- (void)willPresentAlertView:(UIAlertView *)alertView{// before animation and showing view
    NSLog(@"%s",__func__);
}
- (void)didPresentAlertView:(UIAlertView *)alertView{// after animation
    NSLog(@"%s",__func__);
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{// before animation and hiding view
    NSLog(@"%s--index---%ld",__func__,(long)buttonIndex);
    
};
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{ // after animation
    NSLog(@"%s--index---%ld",__func__,(long)buttonIndex);
    
}

// Called after edits in any of the default fields added by the style
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView{
    return YES;
}


//注意:
//1,如果想要自动的退出alertView而不是点击按钮推出,选择相面的方式:(需要设置定时器,定时器的使用可以在定时器章节查看)
[alertView dismissWithClickedButtonIndex:3 animated:YES];
/


三,UIActionSheet的使用:

1,创建actionSheet://你可以在创建的时候决定上面有多少个按钮

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheet" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"Sure?" otherButtonTitles:@"really?", nil];

//创建后如果想要增加按钮也可以:(按钮如果太多,会通过scroll方式展示)
[actionSheet addButtonWithTitle:@"jjjjjjj"];

2,设置UIAlertView样式:(共有四种样式)(不知道为什么我设置的时候好想没有什么作用,以后在研究)


//    UIActionSheetStyleAutomatic        = -1,       // take appearance from toolbar style otherwise uses 'default'
//    UIActionSheetStyleDefault          = UIBarStyleDefault,
//    UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent,
//    UIActionSheetStyleBlackOpaque      = UIBarStyleBlackOpaque ,
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;

3,若还未设置代理,请在这里设置代理,(由于在创建的时候我已经设置过了,这里就不再设置)展示出来

[actionSheet showInView:self.view];

4,实现代理方法:

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%ld",(long)buttonIndex);
    
}

// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)actionSheetCancel:(UIActionSheet *)actionSheet{
    NSLog(@"break");
}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{ // before animation and showing view
    NSLog(@"%s",__func__);
    
}

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet{// after animation
    NSLog(@"%s",__func__);
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{ // before animation and hiding view
    NSLog(@"%s--index---%ld",__func__,(long)buttonIndex);
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{// after animation
    NSLog(@"%s--index---%ld",__func__,(long)buttonIndex);
    
}

//注意:
//1,如果想要自动的退出alertView而不是点击按钮推出,选择相面的方式:(需要设置定时器,定时器的使用可以在定时器章节查看)
[actionSheet dismissWithClickedButtonIndex:1 animated:YES];
//2,cancel在最下面,但是它们的序号跟上面的alertview的排列方式是不一样的,如果按钮是再初始化中创建的,那么从上到下依次从零增加,cancel的index是里面最大的,之后手动添加的,再在上面最大的序号(即cancel的index)的基础上依次增加。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容