一,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)的基础上依次增加。