OC自帶的Alert彈窗

一,UIAlertController的使用:

UIAlertController是一個(gè)控制器,所以需要按照控制器的方式進(jìn)行顯示:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion;

手動銷毀的時(shí)候也需要按照控制器的方式進(jìn)行退出:
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion

下面將詳細(xì)介紹一下這個(gè)控制器的使用:

1,創(chuàng)建控制器

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

2,添加按鈕,并為按鈕設(shè)置樣式//可以添加很多個(gè),隨意,通過后面的block進(jìn)行回調(diào),無需代理方法

//這一步是為了防止循環(huán)引用
__weak typeof(alertController) weakAlert = alertController;

UIAlertAction *actionDefault = [UIAlertAction actionWithTitle:@"action Default" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"action Default did clicked");
    //為了防止循環(huán)引用,在這里需要進(jìn)行弱引用設(shè)置,下面如果有用到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,添加文本框//可以添加很多個(gè),隨意,文本框的屬性可以在后面的block中設(shè)置

[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,創(chuàng)建view:

//你可以在創(chuàng)建的時(shí)候決定上面有多少個(gè)按鈕
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is an alert" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"Are you sure?",@"ddd",@"fad", nil];
//創(chuàng)建后如果想要增加按鈕也可以:(按鈕如果太多,會通過scroll方式展示)
[alertView addButtonWithTitle:@"zhhhhhh"];

2,設(shè)置UIAlertView樣式:(共有四種樣式)


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

3,設(shè)置代理<UIAlertViewDelegate>,然后展示出來

//其實(shí)在創(chuàng)建的時(shí)候已經(jīng)設(shè)置過代理了,這里不需要再重新設(shè)置了
//    alertView.delegate = self;
[alertView show];

4,實(shí)現(xiàn)代理方法:

//代理方法

// 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而不是點(diǎn)擊按鈕推出,選擇相面的方式:(需要設(shè)置定時(shí)器,定時(shí)器的使用可以在定時(shí)器章節(jié)查看)
[alertView dismissWithClickedButtonIndex:3 animated:YES];
/


三,UIActionSheet的使用:

1,創(chuàng)建actionSheet://你可以在創(chuàng)建的時(shí)候決定上面有多少個(gè)按鈕

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

//創(chuàng)建后如果想要增加按鈕也可以:(按鈕如果太多,會通過scroll方式展示)
[actionSheet addButtonWithTitle:@"jjjjjjj"];

2,設(shè)置UIAlertView樣式:(共有四種樣式)(不知道為什么我設(shè)置的時(shí)候好想沒有什么作用,以后在研究)


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

3,若還未設(shè)置代理,請?jiān)谶@里設(shè)置代理,(由于在創(chuàng)建的時(shí)候我已經(jīng)設(shè)置過了,這里就不再設(shè)置)展示出來

[actionSheet showInView:self.view];

4,實(shí)現(xiàn)代理方法:

// 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而不是點(diǎn)擊按鈕推出,選擇相面的方式:(需要設(shè)置定時(shí)器,定時(shí)器的使用可以在定時(shí)器章節(jié)查看)
[actionSheet dismissWithClickedButtonIndex:1 animated:YES];
//2,cancel在最下面,但是它們的序號跟上面的alertview的排列方式是不一樣的,如果按鈕是再初始化中創(chuàng)建的,那么從上到下依次從零增加,cancel的index是里面最大的,之后手動添加的,再在上面最大的序號(即cancel的index)的基礎(chǔ)上依次增加。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容