TBAlertController 4.0.0

TBAlertController 4.0.0

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布上次发布2021年10月

Tanner Bennett维护。



TBAlertController

Issues Stars Version License Platform

针对想支持iOS 7, 8, 和 9的iOS开发者,对UIAlertControllerUIAlertViewUIActionSheet的统一。

安装

Cocoapods

  • pod 'TBAlertController'添加到Podfile中,然后运行pod install

手动安装

  • 克隆此存储库
  • TBAlertController.hTBAlertController.mTBAlertAction.hTBAlertAction.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中使用TBAlertControllerStyleActionSheetsetButtonEnabled:atIndex:时添加文本字段。
  • 使用iOS 7上的alert视图样式时设置destructiveButtonIndex(因为iOS 7不支持此功能)。
  • 对于以下任意一项传递nil:标题、目标、操作或一个用于buttonAction:的块。你可以将nil传递给addOtherButtonWithTitle:target:action:withObject:方法中的object参数,因为它将仅调用不带object参数的父方法。

许可证

MIT许可证。有关更多详细信息,请参阅LICENSE文件。