TBAlertController
针对想支持iOS 7, 8, 和 9的iOS开发者,对UIAlertController
、UIAlertView
和UIActionSheet
的统一。
安装
Cocoapods
- 将
pod 'TBAlertController'
添加到Podfile中,然后运行pod install
。
手动安装
- 克隆此存储库
- 将
TBAlertController.h
、TBAlertController.m
、TBAlertAction.h
和TBAlertAction.m
添加到您的项目中 - 导入
TBAlertController.h
,根据需要可选导入TBAlertAction.h
。
关于
TBAlertController
尝试尽可能多地为iOS 7类提供直接的替换,并允许您直接添加按钮,而不是首先创建动作对象,从而为iOS 8提供了一个更简单的接口。然而,这个特性即将推出,这样就能一致地与UIAlertController
的接口保持一致。
对于iOS 7的唯一主要区别是,TBAlertController
使用块和目标选择器样式动作代替了代理。由于该项目旨在针对希望最小化涉及iOS 7和8中动作表和提示视图的代码的开发人员,因此不会添加对代理的支持。您可以使用相同的代码为这两个平台编写;TBAlertController
会为您处理其余部分。
功能
- 不再需要多个参会者!也不再需要iOS 7/8/9的条件代码。只有一个警告可以统治所有...
- 通过目标选择器样式的按钮动作(通常可以放在一行),可以实现可重用的方法。
- 尽可能地以iOS 8 API作为替换。虽然方法签名不同,但按预期工作。
- 支持使用
UIAlertViewStyle
为iOS 7和8添加文本字段,以及使用iOS 8的addTextFieldWithConfigurationHandler:
。 - 可以直接使用任何
addOtherButtonWithTitle...
方法添加按钮,或者创建一个TBAlertAction
并添加它。直接添加动作可以让代码更简洁,而创建并添加TBAlertAction
的方式类似于UIAlertController
添加按钮。
示例
TBAlertController *alert = [[TBAlertController alloc] initWithStyle:TBAlertControllerStyleActionSheet];
alert.title = @"Alert";
alert.message = @"This is a message!";
[alert addOtherButtonWithTitle:@"OK" buttonAction:^(NSArray *strings) { [self pressedOK]; }];
[alert addOtherButtonWithTitle:@"Delete" target:self action:@selector(selfDestruct)];
[alert setCancelButtonWithTitle:@"Cancel"];
alert.destructiveButtonIndex = 1;
这将创建一个包含标题Alert
和消息的动作表,以及三个按钮:带有块样式动作的OK
按钮、带有目标选择器样式动作的Delete
按钮(同时也是“破坏性”按钮),以及仅关闭警告的Cancel
按钮。如果使用任一setCancelButton...
方法设置,则取消按钮总是出现在按钮列表的末尾。
可以使用showFromViewController:
或showFromViewController:animated:completion:
显示警告。
可以为任何按钮添加动作,无论是块的还是目标选择器样式。破坏性按钮只能在iOS 7使用TBAlertControllerStyleActionSheet
时添加。还可以仅使用标题添加按钮。
目标选择器样式动作也支持传递单个参数,如performSelector:withObject:
。
TBAlertController *alert = [[TBAlertController alloc] initWithTitle:@"Title"
message:@"Hello world"
style:TBAlertControllerStyleActionSheet];
[alert addOtherButtonWithTitle:@"Dismiss"];
[alert addOtherButtonWithTitle:@"Say hi" target:self action:@selector(say:) withObject:@"hi"];
还可以在使用警告视图样式时添加文本字段。iOS 7仅支持使用UIAlertViewStyle
添加文本字段,而iOS 8可以添加文本字段,使用UIAlertViewStyle
或通过addTextFieldWithConfigurationHandler:
。所有文本字段的文本都会作为一个数组传递到按钮动作块中。要使用UIAlertViewStyle
添加预定义文本字段,请设置alertViewStyle
属性(默认为UIAlertViewStyleDefault
,没有任何效果)。在iOS 8上,您可以使用alertViewStyle
属性同时与addTextFieldWithConfigurationHandler:
一起使用;使用alertViewStyle
属性添加的文本字段始终出现在警告的顶部。
TBAlertController *alert = [[TBAlertController alloc] initWithStyle:TBAlertControllerStyleAlertView];
[alert addOtherButtonWithTitle:@"Log input" buttonAction:^(NSArray *strings) {
NSString *first = strings[0];
NSString *second = strings[1];
NSLog(@"%@ %@", first, second);
}];
// iOS 8 only
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"This will appear as the second text box";
}];
// iOS 7 and 8
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
注意事项
以下将引发异常
- 调用仅在iOS 8中可用的任何方法,例如
NS_AVAILABLE_IOS(8_0)
,包括addTextFieldWithConfigurationHandler
。 - 在iOS 7中使用
TBAlertControllerStyleActionSheet
和setButtonEnabled:atIndex:
时添加文本字段。 - 使用iOS 7上的alert视图样式时设置
destructiveButtonIndex
(因为iOS 7不支持此功能)。 - 对于以下任意一项传递
nil
:标题、目标、操作或一个用于buttonAction:
的块。你可以将nil
传递给addOtherButtonWithTitle:target:action:withObject:
方法中的object
参数,因为它将仅调用不带object
参数的父方法。
许可证
MIT许可证。有关更多详细信息,请参阅LICENSE文件。