建议把这个放在你的baseController里面,需要用的时候非常的容易
//baseController.h
- (void)showAlertControllerWithTitle:(NSString *)title
message:(NSString *)message
actionNames:(NSArray<NSString *> *)actionNames
actionIndexCallBack:(void(^)(NSInteger actionIndex))actionCallBlock;
//baseController.m
- (void)showAlertControllerWithTitle:(NSString *)title
message:(NSString *)message
actionNames:(NSArray<NSString *> *)actionNames
actionIndexCallBack:(void (^)(NSInteger actionIndex))actionCallBlock {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
for (int i = 0; i < actionNames.count; i ++) {
UIAlertAction *action = [UIAlertAction actionWithTitle:actionNames[i]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
actionCallBlock(i);
}];
[alertController addAction:action];
}
[self presentViewController:alertController animated:YES completion:nil];
}
写好的方法就可以直接拿出来使用了,使用起来就非常方便了
// NSArray *actionNames = @[@"111",@"222",@"333",@"444"];
NSArray *actionNames = @[@"取消",@"确定"];
[self showAlertControllerWithTitle:@"温馨提示"
message:@"你好"
actionNames:actionNames
actionIndexCallBack:^(NSInteger actionIndex) {
NSLog(@"%@",actionNames[actionIndex]);
if (actionIndex == 0) {
//do your work
}else if (actionIndex == 1) {
//do your work
}
//....
}];












网友评论