UIAlertView和UIActionSheet

IOS提示框大概有两种方法,一个是UIAlertView另一个是UIActionsheet。

UIAlertView:

1
2
3
4
5
6
7
8
9
10
11
12
//swift
func showAlertView(){
var alert = UIAlertView()
alert.title = "Title"
alert.message = "message"
//设置代理
alert.delegate = self
alert.addButtonWithTitle("canel")
alert.addButtonWithTitle("OK")
alert.cancelButtonIndex = 0
alert.show()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
//OC
- (void)showAlerView{
UIAlertView *alert = [[UIAlertView alloc] init];
alert.title = @"Title";
alert.message = @"message";

///设置代理
alert.delegate = self;
[alert addButtonWithTitle:@"cancel"];
[alert addButtonWithTitle:@"OK"];
alert.cancelButtonIndex = 0;
[alert show];
}

UIAlertView

设置delegate需要在class后面加UIActionSheetDelegate

1
2
//swift
class ViewController: UIViewController ,UIAlertViewDelegate
1
2
//OC
@interface ViewController : UIViewController<UIAlertViewDelegate>

然后通过这个函数检测用户点击了哪个button

1
2
3
4
5
6
7
8
9
10
//swift
/**
UIAlerViewDelegate

:param: alertView 包含所点击按钮的alert view.
:param: buttonIndex 所点击的按钮在该alert view中的索引号(index),索引号从0开始
*/
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {

}
1
2
3
4
5
6
7
8
9
//OC
/**
*
* @param alertView 包含所点击按钮的alert view.
* @param buttonIndex 所点击的按钮在该alert view中的索引号(index),索引号从0开始
*/
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"%ld",(long)buttonIndex);
}

UIActionSheet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//swift
func showActionSheet(){
var actionSheet = UIActionSheet()
//设置代理
actionSheet.delegate = self
actionSheet.title = "Title"
actionSheet.addButtonWithTitle("cancel")
actionSheet.addButtonWithTitle("Action1")
actionSheet.addButtonWithTitle("destructiveButton")

///让destructiveButton变为红色,"destructiveButtonIndex"
actionSheet.destructiveButtonIndex = 2
actionSheet.cancelButtonIndex = 0
actionSheet.showInView(barButton, animated: true)

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//OC
- (void)showActionSheet{
UIActionSheet *actionSheet = [[UIActionSheet alloc] init];

///设置代理
actionSheet.delegate = self;
actionSheet.title = @"Title";
[actionSheet addButtonWithTitle:@"cancel"];
[actionSheet addButtonWithTitle:@"Action1"];
[actionSheet addButtonWithTitle:@"destructiveButton"];

///让destructiveButton变为红色,"destructiveButtonIndex"
actionSheet.destructiveButtonIndex = 2;
actionSheet.cancelButtonIndex = 0;
[actionSheet showInView:self.view];

}


上面那个Title是通过这行代码加上去的,实在恶心……

1
2
//swift
actionSheet.title = "Title"

一般我不用……仔细一想我好像从来没用过~

设置delegate需要在class后面加UIActionSheetDelegate

1
2
//swift
class ViewController: UIViewController ,UIActionSheetDelegate
1
2
//OC
@interface ViewController : UIViewController<UIActionSheetDelegate>

然后通过这个函数检测用户点击了哪个button

1
2
3
4
5
6
7
8
//swift
/**
:param: actionSheet 包含所点击按钮的action sheet.
:param: buttonIndex 所点击的按钮在该action sheet中的索引号(index),索引号从0开始
*/
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {

}
1
2
3
4
5
6
7
8
9
//OC
/**
*
* @param actionSheet 包含所点击按钮的action sheet.
* @param buttonIndex 所点击的按钮在该action sheet中的索引号(index),索引号从0开始
*/
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

}

不过IOS8之后苹果又新出了个叫做UIAlertController的东西统一了UIAlertView和UIActionSheet。

写到现在终于写了一篇我自己稍微知道点的东西了😂,想写点东西怎么就那么难……

打赏